Bookmark and Share

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]

st: RE: RE: Date conversion question


From   Amy Dunbar <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: RE: RE: Date conversion question
Date   Thu, 10 Mar 2011 14:16:26 +0000

Thank you, Nick and Scott. Although I have a better understanding of how to change the format of the date so it makes sense to me, I still struggle with the detail in the -help dates and times-. Recall that my date variable is "rssd9999."

I used Scott's code as follows:
. gen double mydate = date(string(rssd9999,"%12.0g"), "YMD") 
. gen double year = year(mydate)
. gen double month = month(mydate)

I was unsuccessful when I tried to use the dofc command.
When I tried the following, my year was always 1960
. format rssd9999 %td  //I tried %tc, %td, and %ty - same result
. gen double year =year(dofc(rssd9999))

When I tried the following, my year was always 0.
. format rssd9999 %tc
. gen double year = dofc(rssd9999)

Amy Dunbar

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: Wednesday, March 09, 2011 3:28 PM
To: '[email protected]'
Subject: st: RE: Date conversion question

Don't apologise. If your question is reasonable, just post it. Otherwise, don't! 

Let's work through this step by step. 

Your -rssd9999- has values like 2e7. 

It's evidently a date-time measured in milliseconds. 

. di %tc 2e7
01jan1960 05:33:20

It is a date-time to you, but you need a sensible display format for it. In fact, you have one. 

Next off, -year()- is a function that maps daily dates to years. So, first you need to map date-times to days before you can call up -year()-. That is done by -dofc()-. The function calls can be nested: 

. di %td dofc(2e7)
01jan1960

. di %ty year(dofc(2e7))
1960

What's wrong is that -year()- looks at your variable and can only think that it's in days, indeed 2e7 days after 1 January 1960. That's about 55,000 years from now, and Stata isn't that far-sighted. 

Nick
[email protected] 

Amy Dunbar

Sorry to bother the list with a trivial date question, but I can't figure out I read out how to solve my problem.
How do I get a date variable (rssd9999) to show the actual date instead of 2.01e+07?

I tried to pull out the year with
                         gen year = year(rssd9999) but the generated variable is ".", so the commands suggested in the date and time help file don't work for this variable.

I show the results of tab and codebook for this variable below.

.               tab rssd9999

                Date      Freq.     Percent                Cum.
                                                                
                2.01e+07              1,218     4.47        4.47
                2.01e+07              5,689     20.88     25.35
                2.01e+07              1,208     4.43        29.78
                2.01e+07              5,669     20.80     50.58
                2.01e+07              1,208     4.43        55.01
                2.01e+07              5,516     20.24     75.26
                2.01e+07              1,215     4.46        79.71
                2.01e+07              5,528     20.29     100.00
                                                                
                Total      27,251   100.00

. codebook rssd9999

----------------------------------------------------------------------------------------------------------------------
rssd9999                                                                                                          Date
----------------------------------------------------------------------------------------------------------------------

                  type:  numeric (double)

                 range:  [20070331,20081231]          units:  1
         unique values:  8                        missing .:  0/27251

            tabulation:  Freq.  Value
                          1218  20070331
                          5689  20070630
                          1208  20070930
                          5669  20071231
                          1208  20080331
                          5516  20080630
                          1215  20080930
                          5528  20081231


*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index