-1

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
common sense
  • 3,775
  • 6
  • 22
  • 31
san
  • 1
  • 2
  • `21-30` doesn't mean between 21 and 30 in Stata. At most it means 21 minus 30; here Stata can't handle it. – Nick Cox Jun 21 '21 at 16:10

1 Answers1

3

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.

TheIceBear
  • 2,912
  • 9
  • 23