Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Leap years in computing age


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: Leap years in computing age
Date   Wed, 26 Oct 2011 13:15:40 +0100

"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 
[email protected] 

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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index