Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: decimal years


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: decimal years
Date   Fri, 11 Aug 2006 16:56:58 +0100

In the same spirit, the third line should 
also be corrected. 

gen daysinyear = doy(mdy(12,31,year)) 

The leap year question is discussed in a FAQ. 

FAQ     . . . . . . . . . . . . . . . . . . . . . . . . . Leap year indicators
        1/04    How do I identify leap years in Stata?
                http://www.stata.com/support/faqs/data/leapyear.html

shows that there are lots of ways of checking for a leap year. They
are all at least a little fiddly, but not challenging in principle. My favourite 
is that 

doy(12,31,year) == 366 

is 1 or true for a leap year and 0 or false otherwise. Depending 
on details, you may need to plug something into the "year" 
slot. On the whole, there is little need to -generate- a variable 
for this, at least in my experience. You do need a logical expression 
as part of some argument. 

I don't think this really is "frequently asked" or "frequently answered", 
but it seemed worth writing up. 

On the original, I still can't see a consistent interpretation 
for the fractional part of 

1932.2880
1949.0410
1939.4270

Thus if .288 means 28 August, what does .427 mean?
If .288 means day of year 288, same question! 

Nick 
n.j.cox@durham.ac.uk 

Jeph Herrin
 
> Oops, that third line should be reversed or even simplified:
> 
>    gen daysinyear=doy(12,31,year)
> 
> the point being that Stata doesn't have (as far as I know)
> a function for identifying leap years, though it shouldn't
> be difficult to write one I suppose.
 
Jeph Herrin

> > If your variable is -date-, the only tricky part is getting
> > the number of days in the year. Try:
> >
> > gen year = round(date,0)
> > gen daysinyear=mdy(1,1,year)-mdy(12,31,year)
> > gen day = round((date-year)*daysinyear,0)
> > gen newdate=mdy(1,1,year)+day
> >
> > Then -newdate- should contain the date in Stata's elapsed 
> date format.

Caleb Southworth 

>> How can decimal years be read into stata? The data are 
> %9.4f and identify
> >> year, month and day, for example
> >>
> >> event1
> >> 1932.2880
> >> 1949.0410
> >> 1939.4270
> >>
> >> I would like to be able to extract the mdy and to be able 
> to tsset my
> >> data, the latter requiring integers.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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