

#LABEL DEFINE STATA SERIES#
Local mylabels `" "Time series of apples, sum, %" "Average of pears, over 1 year" "Maximum of bananas, over 2 years" "' local myvar `" "Apples_ts_sum" "Pears_avg_1y" "Bananas_max_2y" "' Taking your example data, I created another local containing the variable names. Let's say your 60 variables can be generally listed with the expression EXP. 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. I ran into this entry by Nick Cox: 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" "' "Time series of apples, sum, %" "Average of pears, over 1 year" Some example data (excuse the randomness): gen Apples_ts_sum =. 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.

I managed to create a loop, similar to the method used here. Do I have to store my variable range globally and then use a foreach? Or do I use forvalues?Įdit: I was referring to variable labels. However I am struggling with the exact expression of the loop. So for example local mylabels "dog cat bird" I want to assign labels to all variables, which I stored locally. Gen electron_right=(electron=1) if electron<.Let's say I have 60 variables, none with similar naming patterns. Gen bigbang_right=(bigbang=1) if bigbang<. Label define edcats 1 "Less than HS" 2 "HS" 3 "Some College" /// Label variable edu_cat "Education Category" Gen edu_cat=1 if educ12 & educ16 & educ12 & educ16 & educ<. if conditions ensure that each respondent gets the right value of edu_cat based on its value of educ. You'll need an initial gen command to create the new variable and handle one of the categories, and a replace command for each of the remaining categories. Let's turn the educ variable ("HIGHEST YEAR OF SCHOOL COMPLETED") into a categorical variable edu_cat, with the categories "Less than High School", "High School", "Some College", "Bachelors", and "Advanced". With replace, an observation that doesn't meet the if condition is left unchanged. With gen, an observation that doesn't meet the if condition will not get a value for the new variable-it will be missing instead. The gen and replace commands will often have if conditions. The something you're setting the variable to will be the result of some math, but it can be really simple math, like a single number. The basic syntax is the same for both commands: Since replace can destroy data, it has no abbreviation.

You can change the value of an existing variable using replace. You create a new variable in Stata using the generate command, usually abbreviated gen.

Start with the usual setting up (see Doing Your Work Using Do Files):
#LABEL DEFINE STATA HOW TO#
In this article you'll learn how to create new variables and change existing variables.Īssuming you created an SFS folder while reading Managing Stata Files, go to that folder and create a new do file called newvars.do. If you are new to Stata we strongly recommend reading all the articles in the Stata Basics section. This article is part of the Stata for Students series.
