Re: st: generate age variable from year and month of birth and date of the survey

Re: st: generate age variable from year and month of birth and date of the survey

Date

Thu, 28 Mar 2013 10:08:00 +0100

On Thu, Mar 28, 2013 at 3:55 AM, Blais, Martin wrote:
> I need to create an age variable based on year and month of birth and
> date of the survey.
>
> The three variables are :
> bdaymonth (1-12)
> bdayyear (yyyy)
> timestamp (%tc; displaying 21mar2013 18:46:25)
First, I would turn timestamp in a monthly date variable (see: -help
datetime):
gen mtimestamp = mofd(dofc(timestamp))
format mtimestamp %tm
Second, I would create bday as a monthly date variable (see: -help datetime-):
gen mbday = ym(bdayyear,bdaymonth)
format mbday %tm
Third, the difference between the two is age in months:
gen mage = mtimestamp - mbday
Fourth, age in years is the age in months divided by 12 rounded down:
gen age = floor(mage/12)
The -format- commands I added to this list are not strictly necesary;
the only thing these commands do is make the variables mtimestamp and
mbday easier to read for humans. So these commands help if you want to
do this step by step and inspect the variables you create (certainly a
good idea), but they won't change the final result.
Hope this helps,
Maarten
---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany
http://www.maartenbuis.nl
---------------------------------
*
* 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/