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: date function
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: date function
Date
Tue, 6 Aug 2013 16:18:06 +0100
Working backwards:
1. Changing the format of anything has precisely no effect on what is
stored, only on what is displayed. Thinking that "format" means
"display format" and nothing else is crucial to understanding this.
There is a Tip spelling this out in excruciating detail.
http://www.stata-journal.com/article.html?article=dm0067
So no machinations with -format- will solve this problem.
2. My guess is that your -date- variable is thoroughly messed up and
it was produced in some wild way. I guess further that you used
-encode- to convert a string variable into a numeric variable with
labels. If you look at the dates that Stata thinks are stored they are
all daily dates in 1960 and, moreover, the first such date in your
sample is
03/01/2003 10 1 1960
and the last such date is
25/04/2003 10 4 1960
which is consistent with this guess. That is, the string "03/01/2003"
is the first alphabetically (alphanumerically) and the string
"25/04/2003" is the last alphabetically in this sample and they, and
the other dates, have been mapped to numbers. -encode- is totally
clueless about dates and faced with dates it can only treat them like
any other set of strings, the first alphabetically being mapped to 1,
the second to 2, and so forth, regardless of the strings' meaning or
content.
So, you must go back and recalculate your dates, using the -date()-
function. Ironically, or otherwise, this is the title of your post,
but you appear not to have used it.
As said, these are guesses, but I'd call them informed guesses.
The document to read first, second and third on dates is the help.
-help dates and times-.
Nick
[email protected]
On 6 August 2013 15:52, Dherani, Mukesh <[email protected]> wrote:
> I have a problem getting my date variable sorted. I have Date variable in = my data (float) and I want month and year extracted using
>
> gen month = month(date)..... same for day and yr.
> However, I am getting incorrect values for month and year (even day).
> I have also tried formatting month and year to %m and %ty, but to no avail.
> Any help appreciated... I am using Stata 10. [no luck with FAQ so far]
>
>
> date day month yr
> 19/02/2003 22 3 1960
> 17/01/2003 25 1 1960
> 03/01/2003 10 1 1960
> 23/01/2003 16 2 1960
> 03/01/2003 10 1 1960
> 07/01/2003 7 4 1960
> 10/01/2003 30 1 1960
> 18/07/2003 14 2 1960
> 25/04/2003 10 4 1960
> 05/04/2004 5 2 1960
> 03/01/2003 10 1 1960
> 19/06/2003 21 2 1960
> 13/06/2003 5 1 1960
> 19/08/2003 12 4 1960
>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/