# Re: st: decimal years

 From "Austin Nichols" To statalist@hsphsun2.harvard.edu Subject Re: st: decimal years Date Fri, 11 Aug 2006 11:47:31 -0400

Caleb--
If you want to -tsset-, consider using the obs# instead of the date
(-sort event1-, -gen obs=_n-, then -tsset obs-), for various reasons.
Also, it looks like your date variable is incorrectly defined.  1932
had 366 days, so day 105 of 1932 would be 1932.2869 and day 106 would
be 1932.2896, but you show a date of 1932.2880 which identifies a time
of day as well (perhaps the final zero is false precision, and someone
divided every day-of-year number by 365 so day 105 is coded as
1932.28767 or 1932.288 in your example, which means Dec 31, 1932 will
appear to be in 1933).

Even assuming your date variable has no such problems, Jeph's code
will not work as advertised, but the general strategy works:

input event1
1932.288
1949.041
1939.427
1959.999
1960
1960.002
end
g y=floor(ev)
gen yl=doy(mdy(12,31,y))
gen d=max(round((ev-y)*yl,1),1)
g nd= mdy(1,1,y)+d-1
format nd %d
l, noo clean

On 8/11/06, Jeph Herrin <jherrin@flyingbuttress.net> wrote:
Oops, that third line should be reversed or even simplified:

Jeph Herrin wrote:
> If your variable is -date-, the only tricky part is getting
> the number of days in the year.
>
> Caleb Southworth wrote:
>> 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/