Announcement

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

  • Looping to rename variable names with numbers

    I have a series of variables I would like to rename - they all have a 4-digit year in the middle; however, the number of characters before/after the year are different for each name. I would like to move the 4 digit year to the end of the variable name and truncate that number to only the last 2 digits. For example, I would like to rename ques2011survey to quessurvey11 and example2012b to exampleb12. I have tried using the regex function (similar to the advice on this thread: http://www.stata.com/statalist/archi.../msg01581.html), but I am still struggling. Thanks in advance for any help!

  • #2
    Code:
    clear 
    set obs 1 
    foreach v in ques2011survey ques2012survey example2011b example2012b { 
         gen `v' = 42 
    } 
    
    rename ques20#survey quessurvey#
    
    rename example20#b exampleb#
    
    d
    
    Contains data
      obs:             1                          
     vars:             4                          
     size:            16                          
    ---------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    ---------------------------------------------------------------------------------------------
    quessurvey11    float   %9.0g                 
    quessurvey12    float   %9.0g                 
    exampleb11      float   %9.0g                 
    exampleb12      float   %9.0g                 
    ---------------------------------------------------------------------------------------------
    Sorted by: 
         Note: Dataset has changed since last saved.

    Comment


    • #3
      Wow that is much simpler than my attempts to extract the year with regex, substring out the last 2 digits, and then reattach it to the end... thank you!

      Comment


      • #4
        For a loop I can't see a strong case for regular expressions when this would work too:

        Code:
        clear 
        set obs 1 
        foreach v in ques2011survey ques2012survey example2011b example2012b { 
             gen `v' = 42 
        } 
        
        foreach y in 11 12 { 
             rename ques20`y'survey quessurvey`y' 
             rename example20`y'b exampleb`y' 
        }

        Comment


        • #5
          Hi Mr. Cox,
          I hope you are doing well.
          There is a letter at the end of variable names I want to remove that letter from the end of variable names and add sequential numbers to the end of the variables
          There are some variables having these names q237a, q237b,...., q237ab, q237ac, and so on. I want to change them to q2371, q2372,.......up to the end.
          could any one help me, please?
          Thanks before hand

          Comment


          • #6
            #5 is a duplicate of https://www.statalist.org/forums/for...p-of-variables

            Please ask each question just once, to avoid duplication of effort.

            Comment

            Working...
            X