creating dichotomous variable

creating dichotomous variable

by Julia Ramos -
Number of replies: 3

Hello, 


am working on assignment 4  question 4. based on review of lecture notes and stata help, i'm still having trouble with the syntax for the generate command. I did figure it out with the recode command but it seems like generate should work too, would love some help with the syntax. 


I want to create a new variable, age50, which is 0 when age<50 and 1 when age 50 or greater. so, I have tried multiple versions: 

gen age50 = (0 if age<50 & 1 if age>=50)

gen age50 = 0 if age<50 & 1 if age>=50

if i generate age50 with just one of the conditions, e.g. = 0 if age<50, then i create a bunch of missing data which I could replace with 1 I suppose, but this seems inelegant. I can't figure out the right syntax for the command to give it two conditions. 



In reply to Julia Ramos

Re: creating dichotomous variable

by Atul Kumar -

I don't think you can use gen command and assign multiple values the way you are doing. As you mentioned you can use replace command for the second value. Alternatively,  you can use recode command to specify all the rules in a single command.

recode age (rule 1) (rule2), gen(age50)

Checkout the "help recode" examples.

In reply to Julia Ramos

Re: creating dichotomous variable

by Eduardo Rodriguez Almaraz -

Hello Julia,


You can create a dichotomous variable with the following statement:


generate age50 = age >49.


Keep in mind that this is just to create a dichotomous variable which will work in this specific case. But, if for some reason you need to create a categorical variable is better to use the 'recode' command since you can create the variable and assign labels in "one line of code".



In reply to Eduardo Rodriguez Almaraz

Re: creating dichotomous variable

by Julia Ramos -

Thank you so much, very helpful.