05-22-2013 03:44 PM
Anybody know of SAS code, or mathematical algorithm, that can determine if a date/time value was during Daylight Savings Time or not?
05-22-2013 04:05 PM
Well, yes, that would be nice; although I would settle for code or algorithm that works in Rochester, NY
I should add that the code or algorithm that I hope to find would work going back for at least a decade.
05-23-2013 06:33 PM
Here is how I would code it for years >= 2007 (the change dates were different before). If you want datetimes, you will have to take into account that the switch occurs at 2AM.
format myDate DSTbegin DSTend date9.;
do year = 2007 to 2025;
/* random date */
myDate = intnx("DAY", mdy(1,1,year), floor(rand("UNIFORM")*364));
/* second Sunday of March */
DSTbegin = intnx("WEEK", intnx("DAY",mdy(3, 1, year(myDate)), -1), 2);
/* first Sunday of November */
DSTend = intnx("DAY",intnx("WEEK", mdy(10, 31, year(myDate)), 1), -1);
DST = DSTbegin <= myDate <= DSTend;
proc print; id myDate; run;
You can check the dates against
05-24-2013 08:12 AM
Impressive work! I will have to see if I can adapt this to pre-2007.
By the way, there appears to be errors in DSTend, they are off by a day on a couple of years. I'll have to see if I can come up with a fix
05-24-2013 08:18 AM
DSTend is not the first day of the non-DST period, it is the last DST day, the day before. I choose to use that date so that it could be used in a SQL condition like WHERE date BETWEEN DSTbegin AND DSTend. - PG
05-24-2013 08:20 AM
An earlier version ( Spring forward, fall back: Using SAS to compute the onset of daylight saving time - The DO Loop) used PG's approach.
05-24-2013 11:44 AM
Here is a published sample that accounts for before and after 2007:
Sample 24735: Compute daylight saving time