Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • renaming multi-variables using foreach

    Hi,

    I am asking this clarification after going through STATA doc and some of the web content. Although I could not get the "foreach" for "rename" completely (I am still learning that command to understand more), I felt that the examples given in the doc and web have not helped me. Hence, I am posting here my question is that I would like to run rename command for many variables in one go. For eg. the existing variables b13_q2, b13_q3, and b13_q4 have to berenamed as b13q2, b13q3, and b13q4. I do have to do this for many variables. How I was doing was in MS-excel. But I wanted to do that in STATA. I wanted to know whether "foreach" would work or is there any other command is to be called? Could someone help me outfrom this.


  • #2
    karthick veerapandian since the only thing that varies between your variables is a number, it is more efficient to do with a -forval loop. Does not matter so much in this example with only 3 values, but matters with bigger loops.

    Code:
    forval x = 2/4{
    ren b13_q`x' b13q`x'
    }
    However, when renaming variables, I find it easier just to use wildcards. In you example, you can achieve what you want with:

    Code:
    ren *_* **

    Comment


    • #3
      Hi Alnor,
      Thanks much for your response. I prefer your second command. which is quite easy. Thanks again.

      Comment

      Working...
      X