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]

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


From   "Blais, Martin" <[email protected]>
To   <[email protected]>
Subject   RE: st: generate age variable from year and month of birth and date of the survey
Date   Thu, 28 Mar 2013 10:31:10 -0400

Thank you so much. It works perfectly!

Martin


-----Message d'origine-----
De : [email protected] [mailto:[email protected]] De la part de Maarten Buis
Envoyé : 28 mars 2013 05:08
À : [email protected]
Objet : Re: st: generate age variable from year and month of birth and date of the survey

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/

*
*   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/


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