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
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
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.
If the
age
variable has missing values you probably want to addreplace 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.