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: generating age using dates
From
martine etienne <[email protected]>
To
[email protected]
Subject
Re: st: generating age using dates
Date
Fri, 17 Dec 2010 10:46:52 -0800 (PST)
Great! thanks very much! It worked, the ages look just as I expected them to.
Although I'm not sure what to do with the "missing" value, although we coded it
as 99/99/9999 it did not calculate and age, which makes sense. Perhaps I will
just make a note in the final write up, do you think that will make sense?
----- Original Message ----
From: Nick Cox <[email protected]>
To: "[email protected]" <[email protected]>
Sent: Fri, December 17, 2010 1:24:40 PM
Subject: RE: st: generating age using dates
To use on a variable use a function as part of what is fed to -generate-
. generate q1_dob3 = date(q1_dob, "DMY")
And watch out for your "missing" value.
Nick
[email protected]
martine etienne
thank you very much for the clarity, this command is very clear and makes sense:
di date("31/12/2005", "DMY")
16801
. di %td date("31/12/2005", "DMY")
31dec2005
but am I using this for each individual date? I have 287 samples. ie..
. di date("01/04/1956", "MDY")
-1458
. di %td date("01/04/1956", "MDY")
04jan1956
From: Nick Cox <[email protected]>
Bad news which is also good news: That's quite wrong.
-encode- just maps the ordered dates to integers 1 up. It appears to keep the
date information because you see value labels which look like dates, but
underneath it is just integers with negligible chance of ever being what you
want.
. encode string_date , gen(num_date)
. l
+-------------------------+
| string_d~e num_date |
|-------------------------|
1. | 31/12/2005 31/12/2005 |
2. | 31/12/2006 31/12/2006 |
3. | 31/12/2007 31/12/2007 |
4. | 31/12/2008 31/12/2008 |
5. | 31/12/2009 31/12/2009 |
+-------------------------+
. l , nola
+-----------------------+
| string_d~e num_date |
|-----------------------|
1. | 31/12/2005 1 |
2. | 31/12/2006 2 |
3. | 31/12/2007 3 |
4. | 31/12/2008 4 |
5. | 31/12/2009 5 |
+-----------------------+
So, if you use arithmetic on the numeric variables created they act like dates
from 1 Jan 1960 up, but that's generally going to be very wrong indeed.
You need to use specific date functions. -encode- knows nothing about dates.
Consider -date()-:
. di date("31/12/2005", "DMY")
16801
. di %td date("31/12/2005", "DMY")
31dec2005
Nick
[email protected]
martine etienne
i used the following command:
encode q1_dob, generate (q1_dob2)
From: Nick Cox <[email protected]>
How did you "encode" that?
martine etienne
the variable q1_dob2 is the only one that I generated from encoding the original
variable q1_dob which was is in string format:
des q1_dob
storage display value
variable name type format label variable label
----------------------------------------------------------------------------------------------------------------------
q1_dob str10 %10s
all years are four digits, see below:
tab q1_dob
q1_dob | Freq. Percent Cum.
------------+-----------------------------------
01/04/1956 | 1 0.35 0.35
01/05/1964 | 1 0.35 0.70
01/05/1968 | 1 0.35 1.05
01/06/1956 | 1 0.35 1.39
01/06/1963 | 1 0.35 1.74
01/07/1960 | 1 0.35 2.09
01/10/1975 | 1 0.35 2.44
01/12/1957 | 1 0.35 2.79
also, there is only one missing date of birth in the original variable and it is
coded as 99/99/9999, there
are no missing dates in the survey_date variable.
*
* 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/