0

Let's say I have 60 variables, none with similar naming patterns. I want to assign labels to all variables, which I stored locally. So for example

local mylabels "dog cat bird"  

However I am struggling with the exact expression of the loop. Do I have to store my variable range globally and then use a foreach? Or do I use forvalues?

Edit: I was referring to variable labels. I managed to create a loop, similar to the method used here http://www.stata.com/support/faqs/programming/looping-over-parallel-lists/. However I ran into a more difficult problem: my variables have no particular naming patterns, and the labels have special characters (spaces, commas, %-signs), and here is where my loop does not work.

Some example data (excuse the randomness):

gen Apples_ts_sum = .
gen Pears_avg_1y = .
gen Bananas_max_2y = .

And some example labels:

"Time series of apples, sum, %" "Average of pears, over 1 year" "Maximum of bananas, over 2 years".

I ran into this entry by Nick Cox: http://www.stata.com/statalist/archive/2012-10/msg00285.html and tried to apply the mentioned parentheses method, like so:

 local mylabels `" "Time series of apples, sum, %" "Average of pears, over 1 year" "Maximum of bananas, over 2 years" "'

But could not get it to work.

2
  • 1
    Labels means value labels? variable labels? Probably the second, but if you do not show code a minimal expectation is that you show how you want your data to look. Commented Oct 22, 2015 at 10:38
  • Thanks for the edit that clarified this. In general, the approach of putting all the variable labels into one local macro poses the problem of taking them out again, as you discovered. It can be as easy to use varm interactively, or if you prefer more directly reproducible research to create a do-file defining the variable labels one by one. Commented Oct 26, 2015 at 19:13

1 Answer 1

1

If you want to label all the variables the same thing, for example "dog cat bird", Then you can use the varlist option for the describe command. Let's say your 60 variables can be generally listed with the expression EXP. Then:

    qui des EXP, varlist
    foreach variable in `r(varlist)'{
        label var `variable' "dog cat bird"
    }

Edited: Taking your example data, I created another local containing the variable names.

 local myvar `" "Apples_ts_sum" "Pears_avg_1y" "Bananas_max_2y" "'
 local mylabels `" "Time series of apples, sum, %" "Average of pears, over 1 year" "Maximum of bananas, over 2 years" "'
 forval n = 1/3{
    local a: word `n' of `mylabels'
    local b: word `n' of `myvar'
    di "variable `b', label `a'"
    label var `b' "`a'"
 }

Note that I manually created the list of variables. You can automatically create this list using the method I listed above, with des, varlist.

qui des , varlist
foreach var in `r(varlist)'{
    local myvar_t "`myvar_t' `var'"
}

You can then use the local myvar_t instead of myvar in the above example.

Sign up to request clarification or add additional context in comments.

8 Comments

It seems most unlikely that the OP wants the same variable label for all variables. But if that is so, the describe is dispensable here, as foreach var of var EXP will suffice. However, OP has yet to clarify question.
This is true. If the label should be different for each variable, and there are no systematic similarities, then I cannot imagine something which is not manual.
I guess wildly that the OP has put lots of variable labels into a macro and now wants to take them out again, but speculation is futile; perhaps it's value labels after all. It's the OP's sole responsibility to make clear what is wanted.
Late reply here: see edit in original post for more info.
The edited reply is now helpful, and would be made even better by taking it out the first guess at what was meant.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.