Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: RE: Leap years in computing age

 From Austin Nichols To statalist@hsphsun2.harvard.edu Subject Re: st: RE: Leap years in computing age Date Wed, 26 Oct 2011 11:04:53 -0400

```Chris <statachris@gmail.com> :
calculation is not correct, in that it does not measure age in years
or portions thereof exactly, but presumably you do not want to count
someone as closer to the discontinuity if they are 5 days away in 1980

clear
set obs 5
g birthday=mdy(2,29,1980) in 1/3
g eventday=mdy(2,28,1981) in 1
replace eventday=mdy(3,1,1981) in 2
replace eventday=mdy(3,2,1981) in 3
replace birthday=mdy(2,28,1979) in 4/5
replace eventday=mdy(2,27,1980) in 4
replace eventday=mdy(2,29,1980) in 5
gen Age_years = year(eventday)-year(birthday)
gen Age_days  = doy(eventday) - doy(birthday)
gen age       = Age_years + Age_days/365
gen a2=Age_years+Age_days/(365+!mi(mdy(2,29,year(eventday))))
list

You should measure age in days elapsed, so that whether you divide by
365.x or not is irrelevant:
gen age=(eventday-birthday)
unless you need distance in days from some birthday, e.g. 18 or 65, in
which case you should calculate that directly.

Suppose age 65 is the cutoff, and try:

clear
set seed 1
range bd -24000 -14000 10001
gen ed=ceil(runiform()*10000)
format %td bd ed
drop if ed<bd
gen Age_years = year(ed)-year(bd)
gen Age_days  = doy(ed) - doy(bd)
gen a0=Age_years+Age_days/365
la var a0 "First proposed age measure"
gen a1=Age_years+Age_days/(365+!mi(mdy(2,29,year(ed))))
la var a1 "Second proposed age measure"
gen b2=mdy(month(bd),day(bd),year(bd)+65)
replace b2=mdy(3,1,year(bd)+65) if month(bd)==2&day(bd)==29
gen a2=ed-b2
la var a2 "Age in days relative to 65th birthday"
list if inrange(a2,-1,1)

On Wed, Oct 26, 2011 at 8:15 AM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> "Precise age in years" is not to me a precise, unequivocal concept because fraction of the year elapsed means slightly different things in leap years and non-leap years. Your division by 365 is clearly slightly wrong for leap years and using 365.25 (which I've seen often) is slightly wrong for both. Whether the imprecision matters to you is not clear, but either way conditioning on whether a date is in a leap year is not difficult as missing(mdy(2,29,year)) is 1 in non-leap years and 0 in leap years.
>
> The user-written package -egenmore- (SSC) has an -egen- function -foy()- giving fraction of year for daily dates which is smart about leap years. I wrote it because I wanted a function that could be used for studying seasonality in environmental data. So you could apply it to two daily dates and then calculate the difference. Or you could piratise some of the small code tricks for your purposes.
>
> However, rivers and weather, the sort of things I study, don't care about birthdays, which may be more important for your application.
>
> There is also stuff at
>
> FAQ     . . . . . . . . . . . . . . . . . . . . . . . . . Leap year indicators
>        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
>        1/04    How do I identify leap years in Stata?
>                http://www.stata.com/support/faqs/data/leapyear.html
>
> Nick
> n.j.cox@durham.ac.uk
>
> Chris
>
> I would like to compute people's precise age in years on the day of an
> important event.
> Since I use the age as the forcing variable for an RDD, it is
> important that I compute it as precisely as possible. In particular, I
> want get it right whether individuals have or have not passed the
> threshold number of years.
>
> I have both the event day and the day of birth in Stata format, i.e.
> in days after 1 January 1960.
> Now I reckon I cannot just compute --gen age =
> (eventday-birthday)/365--because of leap years.
> So my approach was the following:
>
> gen Age_years = year(eventday)-year(birthday)
> gen Age_days  = doy(eventday) - doy(birthday)
> gen age       = Age_years + Age_days/365
> drop Age_*
>
> But I'm wondering whether there is a more straightforward approach to
> this, and indeed whether the above approach is correct?

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