Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Changing date of birth to age in data editor


From   Joseph Coveney <[email protected]>
To   Statalist <[email protected]>
Subject   Re: st: Changing date of birth to age in data editor
Date   Tue, 15 May 2007 09:49:27 +0900

Mark Slatyer wrote

Could anyone tell me how to change a data set with dob to present age as a
new variable please.

--------------------------------------------------------------------------------

If you're willing to accept noninteger ages, then the code below will work.
You'll need to make the necessary changes in order to accommodate your
date-of-birth variable's datatype and date format.

If you need age in years as an integer, then it isn't quite as
straightforward.  (See http://support.sas.com/ctx/samples/index.jsp?sid=271
for the algorithm.) You'll be able to use Stata's other date functions for
this, e.g., -day()-, -month()-, -year()-.  See -help date functions-.

Joseph Coveney

set obs 1
generate str dob = "1960-01-01"
generate float age = (date("`c(current_date)'", "dmy") - ///
 date(dob, "ymd")) / 365.25
list
exit

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index