Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: String to date


From   "Nick Winter" <nwinter@policystudies.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: String to date
Date   Wed, 30 Oct 2002 11:23:47 -0500

> -----Original Message-----
> From: cb23 [mailto:cb23@york.ac.uk] 
> Sent: Wednesday, October 30, 2002 11:18 AM
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: String to date
> 
> 
> There is an easier way, two as far as I can see.
> 
> You can split the string up based on
> 
> i) the positioning of the .
> ii) the consistent locations of the numbers.
> 
> So for the simplest case of ii) you could
> 
> compress dob
> gen str2 dob_day = substr(dob,1,2)
> gen str2 dob_month = substr(dob,3,2)
> gen str2 dob_year = substr(dob,5,2)
> destring dob_*, replace
> gen dob1 = mdy(dob_day,dob_month,dob_year)
> format dob1 %d

As Nick Cox might say, this has the advantage of working from first
principles.

You can also use the date()  function, which exists for precisely this
purpose (see help datefcn).

	gen dob1 = date("01.08.05","dmy",2002)

The string "dmy" tells Stata the order of Day, Month, and Year
(indicating that the above date is 1 August, not January 8); the third
argument indicates the maximum year possible...this means that years
"03" through "99" will be taken as twentieth century, and  "00" through
"02" as twenty-first century.

Nick Winter
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index