[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
RE: st: String to date
> -----Original Message-----
> From: cb23 [mailto:firstname.lastname@example.org]
> Sent: Wednesday, October 30, 2002 11:18 AM
> To: email@example.com
> 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
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.
* For searches and help try: