How can I generate AGE categorical/binary variable?

1.4k views Asked by At

I tried using the below code but it throws an error in Stata.

gen agecat=.
replace agecat=1 if age<=20
replace agecat=2 if age==21-30
replace agecat=2 if age==31-40
replace agecat=2 if age>=64
1

There are 1 answers

0
TheIceBear On

Would this work? If not, you must let us know what error you get. It is difficult for us to help you if we do not know what error you get.

gen     agecat = 1
replace agecat = 2 if age >  20
replace agecat = 3 if age >  30
replace agecat = 4 if age >= 64 

If the age variable has missing values you probably want to add replace agecat = . if missing(age). Otherwise all missing values would also be in the last category as numeric missing in Stata is treated as infinite (arbitrarily large), which is certainly larger than 64.