Announcement

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

  • Error with rename using local macros

    I importing many excel files with long variables names and lots of space. The truncated variable names do not work my purposes. So I need to shorten the labels into workable variable names.

    I have gotten this code to work until the rename step, but I am not seeing the (probably very obvious error) in the rename step.

    clear
    set obs 2
    gen ShelteredESHomelessPeoplein = 1
    label variable ShelteredESHomelessPeoplein "Sheltered ES Homeless People in Families, 2007"
    foreach v of varlist _all {
    di "`v'"
    local wholename : variable label `v'
    di "`wholename'"
    local new = substr(subinstr("`wholename'", " ", "",.),1,30)
    di "`new'"
    di "`v'" " " "`new'"
    rename "`v'" " " "`new'"
    }

    Thanks in advance

  • #2
    Change the last line to:

    Code:
     rename `v'  `new'
    
    des, fullnames
    
    Contains data
     Observations:             2                  
        Variables:             1                  
    ------------------------------------------------------------------------------------------
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ------------------------------------------------------------------------------------------
    ShelteredESHomelessPeopleinFam
                    float   %9.0g                 Sheltered ES Homeless People in Families,
                                                    2007
    ------------------------------------------------------------------------------------------
    Roman

    Comment

    Working...
    X