Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

Re: st: how does one automatically calculate the age in years by subtracting the system date from the date of birth?

 From Rebecca Pope To statalist@hsphsun2.harvard.edu Subject Re: st: how does one automatically calculate the age in years by subtracting the system date from the date of birth? Date Sun, 10 Mar 2013 09:37:07 -0500

```A correction first.

gen month_dob=int(1+(13-1)*runiform())
gen day_dob=int(1+(32-1)*runiform())
gen year_dob=int(1964+(1999-1964)*runiform())
gen date_of_birth=mdy(month_dob, day_dob, year_dob)

has the potential to produce an error, albeit with a small
probability. Since you are drawing months and days separately, you can
draw nonsensical combinations like 2/30. A better method is to draw
randomly over the range of dates that you want.

scalar start = mdy(1,1,1964)
scalar end = mdy(12,31,1999)
gen date_of_birth = int(start+(end-start)*runiform())
* the two -scalar- assignments can be skipped & placed directly in
date_of_birth, but I find this clearer (i.e. just style)

You also say:
> I’m encountering some problems subtracting these two
> dates. Could this be due to date formats i.e. month day year (MDY)
> versus day month year (DMY). Or it could be due to one date being an
> integer and the other a string? Or it's something else?

To refer to the _content_ of c(current_date), you must treat it as a
macro: `c(current_date)'. Then use it in the -date()- function to
convert to a date value, because yes, you must have two numeric values
in order to subtract.

gen today_date=date("`c(current_date)'","DMY")

Finally:
> Theoretically subtracting the today_date from date_of_birth should
> give the age,

Not so. You should subtract date_of_birth from today_date. This gives
you the difference in days from which you can calculate age in
(fractional) years. If you don't want to change age until the person
has had a birthday, then use the -int()- function.

gen age = round((today_date - date_of_birth)/365.25,0.1)
gen age2 = int((today_date - date_of_birth)/365.25)

/*** full example ***/
version 12
clear
set obs 5
local sd = date("`c(current_date)'","DMY")
set seed `sd'

scalar start = mdy(1,1,1964)
scalar end = mdy(12,31,1999)
gen date_of_birth = int(start+(end-start)*runiform())

gen today_date=date("`c(current_date)'","DMY")

gen age = round((today_date - date_of_birth)/365.25,0.1)
gen age2 = int((today_date - date_of_birth)/365.25)

format date_of_birth today_date %td
list, noobs
/*** end ***/

Output:
date_of~h today_d~e age age2
-------------------------------------
08sep1983 10mar2013 29.5 29
15jul1965 10mar2013 47.7 47
11dec1970 10mar2013 42.2 42
09jan1978 10mar2013 35.2 35
15oct1987 10mar2013 25.4 25

Regards,
Rebecca

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```