Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Seasons from Dates


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Seasons from Dates
Date   Mon, 28 Nov 2005 18:48:08 -0000

With dates, elegance usually goes out of the window. 

I'd do it the other way round. 

gen season = 1 if date >= mdy(12,21,year(date)) | date < mdy(3,20,year(date))  
replace season = 2 if inrange(date,mdy(3,21,year(date)),mdy(6,20,year(date))  
replace season = 3 if inrange(date,mdy(6,21,year(date)),mdy(9,21,year(date))  

... 

(not tested) 

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

Thomas Speidel
 
> Apologies if this messages will be posted twice, but I'm 
> having difficulties
> posting today from my work account.
> 
> I am attempting to create a "season" variable based on a date 
> variable.  The
> solstice start dates are Dec-21, Mar-21, Jun-21 and Sep-22 
> for Winter(1),
> Spring(2), Summer(3) and Autumn(4) respectively.  I have 
> tried to accomplish
> this using a rather long series of logical operators such as:
> 
> gen month=month(date)
> gen day=day(date)
> gen season=1 if (month>=12 & day>=21) | (month>=1 & (month<=3 
> & day<20))
> ...
> 
> However, this code is not correct as it excludes all days>=20.
> Rather than adding more logical operators to condition upon 
> the dates, I was
> wondering if anyone has a suggestion for a cleaner, more elegant code.

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index