Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Blais, Martin" <blais.martin@uqam.ca> |
To | <statalist@hsphsun2.harvard.edu> |
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 : owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] De la part de Maarten Buis Envoyé : 28 mars 2013 05:08 À : statalist@hsphsun2.harvard.edu 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/