-1

I used the following code but all I got was '.' for all age categories

gen agenew=int((birthyear)/365.25)
gen agenew = date("7/16/2020", "MDY") - date("birthyear", "MDY")
replace agenew=agenew/365.25
replace agenew=floor(agenew)```
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
san
  • 1
  • 2

1 Answers1

1

Making a lot of assumptions as you have not included a lot of details about the format of the data you have and not about the format of age you want, but you should be able to modify this code to make it work:

* Example generated by -dataex-. For more info, type help dataex
clear
input str9 birthyear
"11jun1965"
end

*Convert varialbe from string to date variable
gen birthyear_dateformat = date(birthyear, "DMY")
format birthyear_dateformat %td

*Get the year of birth
gen year = year(birthyear_dateformat)

*Get the age from the year of birth variable
gen age = 2021 - year
TheIceBear
  • 2,912
  • 9
  • 23