Thanks, Bill (and Martin, second post).
To be filed away for the next conversion.
Now I see what was happening. My conversion was counting the dates on a daily basis:
01jan1999, 01feb1999, and so on.
So when I formatted it as %tm I went far away into the future.
Eric
Eric de Souza
Professor, European Economic Studies
Director, Library
College of Europe
Dyver 11
BE-8000 Brugge (Bruges)
Belgium
Tel.: (32.(0)50) 47 72 23
Fax:: (32 (0)50) 47 72 20
http://www.coleurope.eu
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of William Gould, StataCorp LP
Sent: 15 February 2010 17:07
To: [email protected]
Subject: Re: st: Converting date strings into dates
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/
*
* 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/