Announcement

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

  • Renaming group of variables as var

    Hello,

    Can anyone advise how to rename a group as variables as simply var1 var2 var3 etc.

    Currently they all begin as n_2004
    They run as follows n_2004_0_0 n_2004_0_1 n_2004_0_2 etc.

    Thanks

  • #2
    This should work for you:
    Code:
    rename n_2004_0_# var#
    See -help rename group-. I *always* have to re-read the documentation to use this command.

    Comment


    • #3
      See -help rename group-

      Code:
      . clear
      
      . set obs 1
      number of observations (_N) was 0, now 1
      
      . gen  n_2004_0_0  = 1
      
      . gen  n_2004_0_1  = 1
      
      . gen  n_2004_0_2  = 1
      
      . rename  n_2004_0_(#) var(#), renumber(1)
      
      . l
      
           +--------------------+
           | var1   var2   var3 |
           |--------------------|
        1. |    1      1      1 |
           +--------------------+

      Comment


      • #4
        Note that Scott Merryman's code will only change the names of variables n_2004_0_0, ..., n_2004_0_9 but not n_2004_0_10. To match any number of digits at the end of the original names, parentheses should not be used:

        Code:
        rename n_2004_0_# var# , renumber
        There is no harm in specifying renumber(1), i.e., explicitly stating that we want the new numbers to start at 1 (the default).

        Best
        Daniel

        Comment


        • #5
          To match any number of digits at the end of the original names, parentheses should not be used:
          . Good point. Thanks.

          Comment


          • #6
            After n_2004_0_9 it goes to n_2004_1_0 so shall I do

            Code:
              
             rename n_2004_# var# , renumber

            Comment


            • #7
              Try

              Code:
              rename n_2004_#_# var# , renumber sort dryrun
              I have added option sort which sorts the old variable names; I have also added option dryrun, which displays a mapping from old variable names to new variable names. When you are satisfied, remove that option.

              Best
              Daniel

              Comment

              Working...
              X