Announcement

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

  • Renaming underscored variables at once

    Hello,

    I am using Stata 11 and have a set of variables that use underscores, see list below:

    _401
    _402
    _402_1
    _402_2
    _403
    _403a
    _403b

    I would like to rename the variables so that underscores are removed and replaced with an 's' in front of the variables, as shown below:

    s401
    s402
    s402_1
    s402_2
    s403
    s403a
    s403b

    Is there any way to rename this set of variables at once instead of renaming them one by one? This is what I have tried so far:

    foreach v of varlist _401 _402 _402_1 _402_2 _403 _403a _403b {
    2. rename `v' s`v'
    3. }

    and

    foreach v of varlist _401 _402 _402_1 _402_2 _403 _403a _403b {
    2. local V: subinstr local v "_" "", all
    3. rename `v' s`v'
    4. }

    In both cases Stata just added the 's' in front of the '_' (i.e. s_401) and I want to remove the '_' and add the 's'.

    Thank you!
    Carr

  • #2
    Although it is somewhat played down now

    Code:
    renpfix _  s
    is a simple way to do this and is, if I recollect correctly, well documented in Stata 11.

    Let's fix your loop:

    Code:
    foreach v of varlist _* {
         local V: subinstr local v "_" "", all
         rename `v' s`V'
    }
    You defined a useful macro, but never used it.

    Also check out renvars (SJ) and the new rename (in Stata 12/13, when you upgrade). (I need to check whether rename was rewritten for Stata 11 or 12.)


    (This should have been posted in the General forum.)


    Comment


    • #3
      I downloaded renvars (directly in Stata by searching renvars, then clicking on “dm88_1″, and then clicking on “(click here to install)”) and it worked perfectly!

      Many thanks!!!
      Carr

      Comment

      Working...
      X