title: How to rename many variables with string suffixes
body: <p>In Stata, I have a set of variables that all begin with <code>pkg</code>.  In their current state, their endings are numeric: <code>pkg1</code>, <code>pkg2</code>, <code>pkg3</code>, <code>pkg4</code> and so on.  </p>
  
  <p>I need to change all of these variables' endings to strings: <code>pkgmz</code>, <code>pkggmz</code>, <code>pkgsp</code>, <code>pkgsptc</code> etc.</p>
  
  <p>I have a column of these string endings, which I can designate as a local list.</p>
  
  <p>For example:</p>
  
  <pre><code>local croplist mz gmz sp sptc mil cof suk tea ric
  </code></pre>
  
  <p>How do I change the numeric endings to the string endings?</p>
  
  <p>My guess at the code can be found below and the <code>???</code> indicate where I am stumped:</p>
  
  <pre><code>local croplist crops mz gmz sp sptc mil cof suk tea ric
  
  foreach x of varlist pkg* {
      local new1 = substr(`x', 1, 3)
      local new2 = ???
      rename `x' ``new1'`new2''
      label var ``new1'`new2'' "Avg district level `new2' price"
  }
  </code></pre>
  
  <p>I wonder if it would be better to utilize the <code>regexr()</code> command, but can't think of a way to include it.  </p>
  
  <p>Any help is appreciated.</p>