Announcement

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

  • Using rename in a loop

    Hi,

    I have a couple of variable that i want to rename adding: "_usb" in front of each variable. I've read some post with basically the same question but my code still doesn't run.

    Here the code i'm using :

    Code:
    foreach v of varlist ... {
          local lbl =strtoname("usb")
        rename `v' `usb'
    }
    Thanks!
    Last edited by Carmen TD; 19 May 2022, 17:37.

  • #2
    In your code the result of local lbl is never used but you try to rename using a local macro usb that is not defined in your code. If that local macro isn't defined, there will be an error. Even if you defined that local earlier, the code will fail as soon as you try to assign the name inside for the second time.

    Unless you've using a very old Stata,

    Code:
    rename (frog toad newt) (_usb=)
    is a pattern to follow. Just use the names you want instead of frog toad newt.

    No loop should be needed as rename does it for you.

    Comment

    Working...
    X