Announcement

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

  • Saving variable names as variables.

    I am new user to Stata. I am trying to take variables and label them as their variable names with a portion added to the front of them. I thought this would be simple code to write in a loop, but so far no luck.

    This what the relevant chunk of attempted code looks like currently (it's only for one group of variables, I wanted to get it working before trying to apply it to the whole data set):

    foreach var in varlist *cradj {
    local labelname
    `labelname' = `var'
    label variable var "Credibility_Adjusted_MLR `labelname'"
    }

    Initially the code ran without errors, but didn't change anything. After some research and doing some tweaking to the code it now returns r(199) claiming that the third line does not make sense.

    Thank you very much for any help!

  • #2
    You're on the wrong sub-forum. This forum is for mata.

    As for your query, you 3rd line does indeed cause an error. The second line assigns <empty> to the local labelname, so the 3rd line resolves to
    <empty> = var, which looks like an assignment but doesn't have either gen or replace in front of it (which isn't what you want to do anyway).

    Minor changes to your code should fix it :
    Code:
    foreach var in varlist *cradj {
        local labelname `: var label `var''
        label variable `var' "Credibility_Adjusted_MLR `labelname'"
    }

    Comment

    Working...
    X