Announcement

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

  • renaming variables with the values of the first row

    Good morning,

    I have a lot of variables named _var1 ... _var23, and I would like to rename them with the values of _var1[1] ... _var23[1].
    When I try to rename one of them it gives me a variable name invalid or syntax error r(198).
    Could somebody help with this please.

    Thank you,

  • #2
    You can try this

    Code:
    forval j = 1/23 { 
         local try = strtoname(_var`j'[1]) 
         capture rename _var`j'  `try' 
    }
    The problem otherwise will be the occurrence of spaces or other invalid characters or names that are too long or already in use.

    Comment


    • #3
      Difficult to say without seeing your data. Could well be because the values are numeric, and Stata does not allow you to name a variable with a number.
      Please give the exact syntax you tried, and a small data example. Post that example using dataex (http://www.statalist.org/forums/help#stata)

      Code:
      ssc install dataex
      dataex in 1/5

      Comment


      • #4
        Thank you Nick, it is indeed adding numerous "_" for some variables. So I am trying to do something like :
        Code:
        subinstr("__a _a ____a","*_*","_",.)
        to replace the various "_" by only one. could you help with this?

        Jorrit, you are right I gave no example of what the data looks like, it's only strings and spaces no numbers (ex : payments this month)

        Comment


        • #5
          Or I can also delete the more the one spaces before renaming. But I think it will be the same code anyway.

          Comment


          • #6
            I suspect that's doomed as you are likely to lose uniqueness. But here's some technique:


            Code:
            local horrible "a_b__c___d____e______" 
            local try "`horrible'" 
            
            local todo 1 
            while `todo' { 
                local previous "`try'" 
                local try : subinstr local try "___" "_", all 
                local try : subinstr local try "__" "_", all 
                local todo = "`previous'" == "`try'" 
            } 
            
            di "`try'"

            Comment


            • #7
              Thanks Nick.
              I did this it seems to be working as there are no extra spaces in the middle of the names
              Code:
              forval j = 1/23 {
                   local try = strtoname(strtrim(_var`j'[1]))
                   capture rename _var`j'  `try'
              }

              Comment


              • #8
                Just in case you didn't think of this, often variable names in the first row come from importing an excel data set. You can have Stata read the first row as variable names when you do the import. This also means Stata doesn't automatically make all your numeric variables into strings.

                Comment


                • #9
                  Okaay I see, this is good to know, thank you Phil

                  Comment

                  Working...
                  X