Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

RE: st: generating age using dates


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: generating age using dates
Date   Fri, 17 Dec 2010 18:00:38 +0000

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 
n.j.cox@durham.ac.uk 

martine etienne

i used the following command:

encode q1_dob, generate (q1_dob2)

From: Nick Cox <n.j.cox@durham.ac.uk>

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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index