Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Converting date strings into dates


From   wgould@stata.com (William Gould, StataCorp LP)
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Converting date strings into dates
Date   Mon, 15 Feb 2010 10:06:52 -0600

Eric <eric.de_souza@coleurope.eu> writes,

> I have never quite mastered the technique of converting date strings
> into dates.  In this case, I have a string variable of the form:
> 1999Jan 1999Feb and so on, and wish to convert it into a date

One solution:

        . clear

        . input  str10 stringdate
        1. 1999Jan
        2. 1999Feb
        3. end

        . gen ddate = date(stringdate, "YM")

        . list

        . format ddate %td

        . list 

Martin Weiss <martin.weiss1@gmx.de> suggested, 

        . gen double mdate = monthly(stringdate , "YM")

As I have it above, ddate is a daily date (0=01jan1960, 1=02jan1960, ...)
whereas Martin's mdate is a monthly date (0=jan1960, 1=feb1960, ...).
You probably will want to use Martin's method, but be aware of the choice 
and also be aware of how easy it is to switch from one to the other.

Using my solution, if you wanted monthly dates, you would type 

        . gen mdate = mofd(ddate)

Using Martin's solution, if you wwanted daily dates, yoiu would type

        . gen ddate = dofm(mdate)

Whichever you use, you do not need to store results as -double-s.
When using date-and-time, you need to store results as doubles because
in that encoding 0=01jan1960 00:00:00.000, 1=01jan1960 00:00:00.001, ...),
and thus one quickly gets into big numbers where rounding can bite. 

With daily dates, even today (15feb2010) is only 18,308.  With monthly
dates, this month (feb2010) is 601.  


-- Bill
wgould@stata.com
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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   |   Site index