Announcement

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

  • How to change numeric label to variable name?

    Hi, everyone.

    I got population data from World Bank, but I couldn't set variables name of year. Maybe I guess Stata doesn't permit to use numeric variables on first character.

    Anyway,
    The World Bank data form is as follows.
    Click image for larger version

Name:	excel.PNG
Views:	1
Size:	43.0 KB
ID:	1427037


    So, I imported it into Stata.
    Syntax is ". import exc population, cellrange(A4) firstrow"
    Click image for larger version

Name:	br.PNG
Views:	1
Size:	131.0 KB
ID:	1427038


    But, as you can see, the name of variables were imported just alphabet.

    I want to change alphabet to combined form Indicator Code(SP.POP.TOTL) and Year(1960~).
    I know Stata doesn't allow to use period like "SP.POP.TOTL1960" in variables empirically. So, I want to make "SPPOPTOTL1960, SPPOPTOTL1961, ..." of every year variables.

    I can solve these problems by using Excel, but I need Stata code because I have also another data not only population data.

    Thank you for your help! :D

    Inho Lee
    Last edited by Inho Lee; 22 Jan 2018, 18:27.

  • #2
    Welcome to Statalist.

    To fix your data in Excel, notice that the Excel formula
    Code:
    =CONCATENATE("SPPOPTOTL",E4)
    when entered in cell E3 will create
    Code:
    SPPOPTOTL1960
    in cell E3. You can use this to easily create new variable names in Excel, which you can then Copy, then Paste Special > As Values to replace the corresponding cells in row 4.

    Within Stata, a loop like the following will allow you to rename your list of variables.
    Code:
    local y 1960
    foreach v of varlist E-AJ {
    rename `v' SPPOPTOTL`y'
    local y = `y'+1
    }
    where you need to replace the variable name AJ by the name of the last of the variables you need to rename. There are other approaches to doing this rename, but this is I think the easiest to understand.

    With that said, to make your future posts to Statalist more effective, review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    In particular, posting a picture of your data is of no use to someone who wants to help but needs data to test their answer on.

    You wrote
    Maybe I guess Stata doesn't permit to use numeric variables on first character. ...
    I know Stata doesn't allow to use period like "SP.POP.TOTL1960" in variables empirically
    You can read the output of the command help varname to see what the rules are.

    Comment


    • #3
      The built-in function strtoname() may also be helpful.

      strtoname(s[,p])
      Description: s translated into a Stata 13 compatible name

      strtoname() results in a name that is truncated to 32 bytes. Each character in s that is
      not allowed in a Stata name is converted to an underscore character, _. If the first
      character in s is a numeric character and p is not 0, then the result is prefixed with an
      underscore. Stata 14 names may be 32 characters; see [U] 11.3 Naming conventions.

      strtoname("name") = "name"
      strtoname("a name") = "a_name"
      strtoname("5",1) = "_5"
      strtoname("5:30",1) = "_5_30"
      strtoname("5",0) = "5"
      strtoname("5:30",0) = "5_30"
      Domain s: strings
      Domain p: integers 0 or 1
      Range: strings

      Comment


      • #4
        Thank you for your advice. I solved my problem.

        I add another codes from my friend.

        1.
        Code:
        rename (E-BI) (SPPOPTOTL#), addnumber(1960)
        This method is using rename group.

        2.
        Code:
        foreach v of varlist E-BI {
          local lb_var : var label `v'
          rename `v' pop`lb_var'
        }
        This is similar to William's method.

        Comment

        Working...
        X