Announcement

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

  • How to get the remaining letters of a variable

    I have some variables named "x2000" "yz2001"... I want to transform them to "2000x", "2001yz"...

    I used function "substr" first to get the years. Then I want to put these years in front of the letters that remain in the name of variables ("x", "yz",...).

    I do not know how to get this remaining part. I tried the code below but it did not work.

    HTML Code:
    foreach v of varlist _all {
               local new = substr("`v'", -4, 4)
               local old = `v'-`new'
               rename `v' `new'`v'
               }
    Many thanks for your help.

  • #2
    2000x would be an illegal variable name. Variable names must start with a letter or an underscore. Your names are good; you probably need to reshape the data, however.

    Comment


    • #3
      Oh I see. I actually reshaped data before, to have the new variables with all years after them. What I want now is to have another "reshape" to have only the years in the first row of the table, and all the remain such as "x" or "yz" should become a column. It seemed to me that I am going to do "reshape long" with j as "x", "yz",... and 2000, 2001,... as new variables. Of course as you said they are numbers and can not be variables, so I guess I must do some other way...

      Comment

      Working...
      X