Statalist


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

st: Re: turning numbers into dates


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: Re: turning numbers into dates
Date   Sun, 3 May 2009 16:38:02 +0200

<>

The -cond- function would let you condition on the length of the string so you could tailor the command:

-gen mydate = cond(length(stredu1st)>4, ...  )-

I would love to fill in the remainder of this command but I just cannot figure out at the moment why

-di %td  date("197506", "YM")-

works and

-di %td  date("7506", "19YM")-

does not...

HTH
Martin
_______________________
----- Original Message ----- From: "Ekaterina Hertog" <[email protected]>
To: <[email protected]>
Sent: Sunday, May 03, 2009 3:47 PM
Subject: 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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index