Announcement

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

  • Foreach Loop to Rename Variable

    Dear all,

    I was trying to use foreach loop to rename variables, I wanted to names of the variables to be like Capacity1, Capacity2, Capacity3...How ever I got a error message as shown below:

    Click image for larger version

Name:	Screen Shot 2020-01-30 at 1.42.04 AM.png
Views:	1
Size:	118.6 KB
ID:	1534273


    Will appreciate if I could get any help on this!

    Thank you so much,
    Craig

  • #2
    If you are referring to a variable list (i.e., you want Stata expand/unabbreviate a list of variable names), you want

    Code:
    foreach var of varlist varname-varname {
    
    }
    However, you do not need a loop here. You want

    Code:
    rename (varname-varname) (Capacity#) , addnumber
    For future posts, please use CODE delimiters (as I have done) not screenshots to present Stata code and output.

    Best
    Daniel
    Last edited by daniel klein; 30 Jan 2020, 00:47.

    Comment


    • #3
      One good thing about your variable names is that they are evocative. I recommend using that detail in variable labels if you are not doing so already.

      Comment


      • #4
        Hi Daniel,

        Thank you for your help, I appreciate it! It worked perfectly. And will change to the CODE next time, didn't know this rule before.

        Thank you,
        Craig

        Comment


        • #5
          Hi Nick, thank you for your suggestion. That is what I did. :-)

          Comment

          Working...
          X