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]
Re: st: how to get age in number of years, months and days
From
jose maria pacheco de souza <[email protected]>
To
[email protected]
Subject
Re: st: how to get age in number of years, months and days
Date
Mon, 22 Oct 2012 23:26:26 -0200
Em 21/10/2012 02:54, Phil Clayton escreveu:
I've been playing with something like this (beware wrapped lines):
----------------
clear
input str9 d1 str9 d2
24oct1980 19oct2012
30sep1954 5aug1989
31oct1974 10sep2002
31oct1974 30oct2002
31mar1985 30mar1995
29feb2000 28feb2004
29feb2000 1mar2004
end
gen date1=date(d1, "DMY")
gen date2=date(d2, "DMY")
format %td date1 date2
gen years=floor(((ym(year(date2), month(date2)) - ym(year(date1), month(date1))) - (day(date2) < day(date1))) / 12)
gen months=(ym(year(date2), month(date2)) - ym(year(date1), month(date1))) - (day(date2) < day(date1)) - 12*years
gen days=date2 - (dofm(mofd(date1) + 12*years + months) + day(date1) - 1)
list
----------------
Of course handling leap years and birthdays on 29 Feb is a little arbitrary. The code for calculating age in years is based on Dan Blanchette's code at the end of this page:
http://www.ats.ucla.edu/stat//stata/modules/dates.htm
I think Nick's approach of calculating years and days is probably better. However, Nick's code seems to fail with the second last set of dates above.
Phil
Phil:
Thank you for your contribution to the thread.
You and Nick helped very much to understand better the subtleties in
dealing with dates.
josé maria
Jose Maria Pacheco de Souza
Professor Titular (aposentado), Professor Senior
Departamento de Epidemiologia/Faculdade de Saude Publica, USP
Av. Dr. Arnaldo, 715
01246-904 - S. Paulo/SP - Brasil
fones (11)3061-7747; (11)3768-8612
www.fsp.usp.br/~jmpsouza
*
* 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/