Announcement

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

  • Renaming a set of variables with a single command

    Hi there,

    I have a two time period data (baseline in 2014 and midline in 2016). The variables in the baseline survey are in this format: var1, var2, var3, .... The variables in the midline survey are in this format: var1_m, var2_m, var3_m, ....

    I want to rename the variables to this, with corresponding year:
    var1_2014, var2_2014, var3_2014, ........, var1_2016, var2_2016, var3_2016

    I can do it for the midline variables using this command:
    Code:
    rename *_m *_2016
    But how do I rename the bulk of the 2014 baseline variables?

    Thanks!

  • #2
    Dear Samyam, you may use a loop:
    Code:
    forvalues i=1/#{
    rename var`i' var`i'_2014
    }
    2B or not 2B, that's a question!

    Comment


    • #3
      See
      Code:
      help rename group
      for a way to do this in a single command. (Away from Stata at the moment and I can't easily check the syntax or I would give the code, my apologies)

      Comment


      • #4
        Thanks to William's beneficial advice, I check the help code and learn a lot. This may be helpful:
        Code:
        clear
        input double(var1 var2 var3 var1_m var2_m var3_m)
        1 2 3 4 5 6
        end
        rename *_m *_2016
        rename var? =_2014
        2B or not 2B, that's a question!

        Comment


        • #5
          When using rename group I try to limit the matching pattern as much as possible to avoid accidentally renaming variables with names similar to the ones I am targeting, so in this case I prefer to match digits using # rather than arbitrary characters using ?. Also, we do not know if the variables in this case are limited to a single digit following "var" or if they go farther. So with that said, here's some code comparing matching with "?" and with "#" which matches a group of one or more digits.
          Code:
          . describe, simple
          var1      var22     var333    var1_m    va22_m    var333_m  varQ
          
          . rename *_m *_2016
          
          . rename var? =_2014
          
          . describe, simple
          var1_2014    var22        var333       var1_2016    va22_2016    var333_2016  varQ_2014
          Code:
           describe, simple
          var1      var22     var333    var1_m    va22_m    var333_m  varQ
          
          . rename *_m *_2016
          
          . rename var# =_2014
          
          . describe, simple
          var1_2014    var22_2014   var333_2014  var1_2016    va22_2016    var333_2016  varQ

          Comment


          • #6
            Dear William, you are correct. I didn't notice that the variable numbers are very likely to be larger than 9. Anyway, mistakes teach me more lessons and thank you for your correction.
            2B or not 2B, that's a question!

            Comment


            • #7
              That really helped!! Thanks everyone, especially Liu Qiang!!

              Comment

              Working...
              X