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: Converting date strings into dates


From   [email protected] (William Gould, StataCorp LP)
To   [email protected]
Subject   Re: st: Converting date strings into dates
Date   Mon, 15 Feb 2010 10:06:52 -0600

Eric <[email protected]> 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 <[email protected]> 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
[email protected]
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index