Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

st: RE: working with date values

From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: working with date values
Date   Wed, 11 Jul 2012 15:14:31 +0100

David Ashcraft had essentially the same question earlier today. 

You have converted a string which is a daily date and got 8766. That is a daily date in numeric form, as you can check:  

. di %td 8766

Just declaring that this is a monthly date is insufficient to get what you want. You are telling Stata that you have a monthly date that is 8766 months away from January 1960 and that turns out to be a long way into the future. 

You need to convert using -mofd()-. 

In general, there are two points of view here. One is the user's. A user often just has one kind of date input and wants one kind of date to use, and deep down is probably thinking "All I want to do is this; why is it so confusing?". But over many different users needs vary, and that implies lots and lots of functions to get from one to the other. So, the documentation has to be long and complicated to cover all the stuff that people might need. 

The other is StataCorp's. StataCorp is in general very resistant to re-naming anything and we tend to regard that as a very good thing. However, -date()- in particular is emphatically not a function for producing dates in general. I suspect that is often not fully understood. It is a function for producing _daily_ dates from various kinds of string input. Its name goes back to a time when daily dates were the only kinds of dates that Stata handled. 

Another common misunderstanding is a belief that changing the format changes the value. No; it just changes how that value is displayed. So, if you tell Stata to display something as a monthly date, it obliges; as a daily date, it obliges, and so on. 

I suggest 

         gen datem = mofd(date(date, "MDY")) 
         format %tm datem

[email protected] 

Data Analytics Corp.
I'm confused on handling dates.  I imported a dataset on steel shipments 
that has a variable called date.  This is a string with the first date 
value as "01/01/1984"  for Jan 1, 1984.  Using describe, I see that date 
is str10 %10s.  I then did

         gen date2 = date(date, "MDY")

and the first value for date2 is 8766.  Then I did

         format %tm date2

and I expected to see jan1984 after doing a list.  But I got 2690m7.  
When I did

         format %td date2

I got 01jan1984.  I want jan1984.  How do I do this?  What is the 
correct way to convert a string that's in the form "MM/DD/YYYY"?

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index