Announcement

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

  • Loop and rename variables based on incrementally higher number

    I need to loop over 48 variable names and rename them pair_1 pair_2 pair_3 [...] pair_48. However I can't get my counter and loop to work together. I have tried different combos of the following code:

    local counter = 0
    while `counter' < 49 {
    local list M N O P Q R S T U V W X Y Z AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ BA BB BC BD BE BF BG BH
    foreach var of local list {
    local i=`counter'+1
    rename `var' pair_`i'
    }
    }

    Your help is greatly appreciated!

  • #2
    Code:
    rename (M-BH) pair_#, addnumber

    Comment


    • #3
      To add to the ideal answer from Øyvind Snilsberg in post #2 , the documentation and explanation is found in the output of help rename group - there is a link to it from the basic help rename output, but it's good to review it and see all the things possible with rename - it is extraordinarily powerful.

      Incidentally, this is how a loop would have worked - you only need one loop, not two.
      Code:
      local i = 0
      local list M N O P Q R S T U V W X Y Z AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ BA BB BC BD BE BF BG BH
      foreach var of local list {  
          local i=`i'+1
          rename `var' pair_`i'
      }

      Comment


      • #4
        Thank you both! That was helpful in many ways

        Comment

        Working...
        X