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? I have tried the 'foreach' command but it seems that the underscores or letters at the end in some of them (e.g. _402_1 or _403a) create a problem and Stata returns an error.

    Thank you!
    Carr

  • #2
    Try

    Code:
    ren _* s*
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    Stata Version: 17.0 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      p.s. Posts like this should go in the General forum (unless you were just practicing how to use the forums).
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      Stata Version: 17.0 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        Hi Richard,

        It did not work. Stata returned the error " _ ambiguous abbreviation"

        This is what I have tried so far:

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

        and

        foreach v of varlist _503 _504 _506 _507 {
        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'.

        Ps. you can move this to the general forum --- I'm not sure how to do it.

        Thank you,
        Carr

        Comment


        • #5
          I can't move the thread. If my next suggestion doesn't solve the problem, then start a new thread in General (and start most future threads in General as well).

          My code seems to work in Stata 13 but not 11. So, download -renvars- from SSC and try this:

          renvars, presub(_ s)

          Here is an example I ran in Stata 11.2.

          Code:
          . clear all
          
          . corr2data _401 _402 _402_1 _402_2 _403 _403a _403b, n(100)
          (obs 100)
          
          . des, simple
          _401    _402    _402_1  _402_2  _403    _403a   _403b
          
          . renvars, presub(_ s)
          
          . des, simple
          s401    s402    s402_1  s402_2  s403    s403a   s403b
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          Stata Version: 17.0 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6
            Carr Leon posted this question in two different forums. The other thread at http://www.statalist.org/forums/foru...iables-at-once mentions

            1. the new rename, added in Stata 12, and so no use to Carr Leon without upgrading

            2. renvars -- but renvars is not on SSC and must be downloaded from the Stata Journal website.

            3. renpfix

            Comment


            • #7
              Thank you Nick and Richard. 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