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

RE: st: Converting date strings into dates

From   DE SOUZA Eric <>
To   "''" <>
Subject   RE: st: Converting date strings into dates
Date   Mon, 15 Feb 2010 17:27:18 +0100

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 de Souza
Professor, European Economic Studies
Director, Library
College of Europe
Dyver 11
BE-8000 Brugge (Bruges)
Tel.: (32.(0)50) 47 72 23
Fax:: (32 (0)50) 47 72 20

-----Original Message-----
From: [] On Behalf Of William Gould, StataCorp LP
Sent: 15 February 2010 17:07
Subject: Re: st: Converting date strings into dates

Eric <> 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 <> 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
*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2020 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index