Announcement

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

  • Problem Renaming Variables That Contain "."

    Hello,

    I am using the 2018 dataset from Latinobarometro. A lot of variables are named something like "P10STC.B". I have been trying to rename them, but keep getting errors.

    " not allowed in oldname
    Syntax is rename oldname newname
    You used . in oldname, not newname. . is used in newname to indicate that the corresponding wildcard in
    oldname be skipped. E.g., "rename a*b* a.b*" removes what is between a and b; variable ausbvalue would be
    renamed abvalue.
    r(198);"

    Please let me know how I can rename those variables. I know those are invalid names, and Stata does not recognize them. Thank you for your help.

  • #2
    You can use this code, which is adapted from that shown in a post earlier this month by Bjarte Aagnes.
    Code:
    local i 0
    foreach varname of varlist * {
        local ++i
        if ( "`varname'" != ustrtoname("`varname'") ) {
            mata : st_varrename(`i', ustrtoname("`varname'") )
        }
    }
    For your convenience, I've also attached it as a DO-file that you can just run.
    Attached Files

    Comment


    • #3
      Dear Joseph,

      Your code solved my problem. Thank you so much. What is the difference between your code and the code written by Bjarte? Could you tell me how you adapted it? I would like to know how to modify the code just in case I run into a similar problem in the future.

      Comment


      • #4
        I think that when he copied and pasted his code into his post, he neglected to include the line that initializes the local macro variable. I inserted that omitted line of code at the top. Also, I used a shortcut syntax for incrementing the local macro—this is only stylistic and has no advantage.

        Comment


        • #5
          I guess that you don't need to explicitly initialize the local macro variable when you use his syntax for incrementing

          . macro drop _all

          . display in smcl as text `i'


          . local i = `i' + 1

          . display in smcl as text `i'
          1

          .


          but I prefer to initialize variables. It helps to avoid unexpected results.

          Comment


          • #6

            Adding the local i 0 is good advice.

            You can also avoid defining any locals (except a short-lived loop counter):
            Code:
            forvalues i = 1/`c(k)' {
            
                capture mata: st_varrename(`i', ustrtoname(st_varname(`i'))) 
            }
            If one prefer avoiding the capture, use the mata conditional operator:
            Code:
            forvalues i = 1/`c(k)' {
            
                qui mata: st_isname(st_varname(`i')) ? 1 : st_varrename(`i', ustrtoname(st_varname(`i'))) 
            }
            To make an example of a dta with illegal variable names -filefilter- can be used:
            Code:
            tempfile A B
            clear
            set obs 1
            gen byte var1 = 1
            gen byte P10STC_A = 1
            gen byte P10STC_B = 1
            save "`A'" , replace
            filefilter "`A'" "`B'" , from(P10STC_) to(P10STC.)
            use "`B'", clear

            Comment


            • #7
              Dear Joseph and Bjarte,

              Thank you for explaining. I’m still a beginner and now googling some terms, trying to understand what you wrote. I’ll try to learn and understand by playing with Stata. Thank you again for your help. I hope your answers will help those who come to this forum with the same problem I had.

              Comment


              • #8
                Hello,

                (Using Stata/SE 13.1)

                I am also having difficulty renaming a variable that has an inllegal/invalid name:



                rename 21000-0_0 ethnicSG
                syntax error
                In parsing oldname, either nothing or something invalid precedes the dash. The dash syntax is varname-varname, and the variable names cannot
                have wildcard characters in them.
                r(198);



                My dataset was created in R software by a colleague who converted it to .dta file for me to use in STATA.
                I don't know how to use the solution given above for my problem. Where in the following should I put my invalid name (21000-0_0) and my new varname (ethnicSG)
                local i 0 foreach varname of varlist * { local ++i if ( "`varname'" != ustrtoname("`varname'") ) { mata : st_varrename(`i', ustrtoname("`varname'") ) } }

                Thank you for any help!

                Comment


                • #9
                  running
                  Code:
                  qui forvalues i = 1/`c(k)' {
                  
                      mata: st_isname(st_varname(`i')) ? 1 : st_varrename(`i', ustrtoname(st_varname(`i'))) 
                  }
                  will change the illegal names to legal names. Then you can rename the new legal variable names.

                  Example:
                  Code:
                  * make illegal varname ".0ke"
                  tempfile invalid
                  findfile auto.dta
                  filefilter "`r(fn)'" `invalid' ,  from("make") to(".0ke")
                  use  *e using `invalid' , clear
                  describe  
                  
                  * find and change illegal names
                  
                  qui forvalues i = 1/`c(k)' {
                  
                      mata: st_isname(st_varname(`i')) ? 1 : st_varrename(`i', ustrtoname(st_varname(`i'))) 
                  }
                  
                  * describe variables names after changing illegal names
                  
                  describe  
                  
                  * rename to the wanted name(s)
                  
                  rename _0ke  make
                  describe
                  Code:
                  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                                storage   display    value
                  variable name   type    format     label      variable label
                  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                  .0ke            str18   %-18s                 Make and Model
                  price           int     %8.0gc                Price
                  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                  
                  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                                storage   display    value
                  variable name   type    format     label      variable label
                  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                  _0ke            str18   %-18s                 Make and Model
                  price           int     %8.0gc                Price
                  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                  
                  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                                storage   display    value
                  variable name   type    format     label      variable label
                  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                  make            str18   %-18s                 Make and Model
                  price           int     %8.0gc                Price
                  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

                  Comment


                  • #10
                    Thank you. Before you replied I actually saved as .csv file then changed the name in excel before importing back into stata, which worked.

                    Thank you for you help - although I am not sure which bits of your code I would have used. Sorry I am very much a novice STATA user!

                    Comment

                    Working...
                    X