Statalist


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

Re: st: AW: turning numbers into dates


From   Ekaterina Hertog <ekaterina.hertog@sociology.ox.ac.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: AW: turning numbers into dates
Date   Sun, 03 May 2009 22:44:07 +0100

Dear all,
thank you so very much for all the brilliant advice!
I have tried different options suggested by Eric, Martin and Tirthankar and finally opted for a slightly modified version of Martin's suggestion as it seemed it would be most flexible, even allowing em to take into account the fact that some of my dates come from 20th and some for the 21st century (I could do it with the topyear option)
Just in case you might be interested this is what I did:

clear
input str10 stredu1st
200109
197104
197504
196504
196904
8804 
8404 
0202
6304 
8304 
end

compress

gen mydate=cond(length(stredu1st)>4, stredu1st, substr(stredu1st,1,2)+" "+substr(stredu1st,3,4))
gen edu1st = cond(length(stredu1st)>4, date(mydate, "YM"), date(mydate, "YM" , 2007))
format edu1st %td

list stredu1st mydate edu1st, noobs

Thank you so very much for your help, 
Sincerely yours,
Ekaterina


In message <000601c9cc1c$d8638080$892a8180$@weiss1@gmx.de> statalist@hsphsun2.harvard.edu writes:
> 
> <> 
> 
> So it  seems Ekaterina can employ several methods, based on the -cond-
> function. She can read up on best practice with regard to this function in
> Nick`s column http://www.stata-journal.com/sjpdf.html?articlenum=pr0016
> 
> If she is willing to make sure that all her two digit years hail from the
> 20th century, she could 
> 
> 
> *************
> clear*
> 
> input edu_start_date_1  // :mylabel , auto // str10 double byte
> 197104
> 197504
> 196504
> 196904
> 8804 
> 8404 
> 6304 
> 8304 
> end
> 
> compress
> 
> tostring edu_start_date_1, gen(stredu1st)
> 
> gen mydate=cond(length(stredu1st)>4, ///
> stredu1st, ///
> substr(stredu1st,1,2)+" "+substr(stredu1st,3,4))
> 
> gen edu1st = cond(length(stredu1st)>4, ///
> date(mydate, "YM"), ///
> date(mydate, "19YM"))
> 
> format edu1st %td
> 
> list edu_start_date_1 mydate edu1st, noobs
> *************
> 
> force a hole into the string after the year digits, and the date function
> would understand her. 
> 
> Alternatively, she could make the same assumption and add the "19" to all
> four digit strings:
> 
> ******************
> clear*
> 
> input edu_start_date_1  // :mylabel , auto // str10 double byte
> 197104
> 197504
> 196504
> 196904
> 8804 
> 8404 
> 6304 
> 8304 
> end
> 
> compress
> 
> tostring edu_start_date_1, gen(stredu1st)
> 
> gen mydate=cond(length(stredu1st)>4, ///
> stredu1st, ///
> "19"+stredu1st)
> 
> gen edu1st = date(mydate, "YM")
> format edu1st %td
> 
> list edu_start_date_1 mydate edu1st, noobs 
> ******************
> 
> She should check very carefully whether the results match her expectations
> :-)
> 
> 
> 
> HTH
> Martin
> 
> -----Ursprüngliche Nachricht-----
> Von: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Ekaterina
> Hertog
> Gesendet: Sonntag, 3. Mai 2009 15:48
> An: statalist@hsphsun2.harvard.edu
> Betreff: st: turning numbers into dates
> 
> Dear all, 
> I have got a variable containing the month and year an individual started
> his or her education. Only Stata thinks the values in this variable are
> numbers and I want to turn them into dates.
> If all the numbers followed the same pattern that will not be a problem.
> 
> for example I could do it like this:
> tostring edu_start_date_1, gen(stredu1st)
> gen edu1st = date(stredu1st, "YM")
> 
> My problem is that while most dates in my dataset come in the yyyymm
> pattern:
> e.g.
>       +----------+
>        | stredu~t |
>        |----------|
>     1. |        . |
>     2. |   197104 |
>     3. |   197504 |
>     4. |   196504 |
>     5. |   196904 |
>        |----------|
> 
> several contain only yymm
> e.g.
> 
>        +-----------+
>        | edu_st~1  |
>        |-----------|
> 12338. |     8804  |
> 13265. |     8404  |
> 13666. |     6304  |
> 13831. |     8304  |
>        +-----------+
> 
> So when I run 
> 
> gen edu1st = date(stredu1st, "YM")
> 
> all the yymm values in stredu1st are turned into missing values in edu1st.
> 
> I could of course edit the values containing only yymm into yyyymm pattern
> manually, but this feels imprecise and prone to error and I would like to
> automate the process if at all possible.
> Is there a way to make the date command recognise alternating patterns?
> I would be very grateful for any advice,
> Sincerely yours,
> Ekaterina
> 
> -- 
> Ekaterina Hertog (nee Korobtseva)
> Nissan Institute of Japanese Studies
> 27 Winchester Road, Oxford
> OX2 6NA
> 
> *
> *   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/

-- 
Ekaterina Hertog (nee Korobtseva)
Nissan Institute of Japanese Studies
27 Winchester Road, Oxford
OX2 6NA


*
*   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   |   What's new   |   Site index