Anybody know of SAS code, or mathematical algorithm, that can determine if a date/time value was during Daylight Savings Time or not?
Thanks.
Anywhere on Earth?
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.
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.
data want(drop=year);
format myDate DSTbegin DSTend date9.;
call streaminit(23356);
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;
output;
end;
run;
proc print; id myDate; run;
You can check the dates against
http://en.wikipedia.org/wiki/Daylight_saving_time_in_the_United_States
PG
Thanks!
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
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
See Computing the onset and end of daylight saving time in SAS: The EASY way! - The DO Loop
An earlier version ( Spring forward, fall back: Using SAS to compute the onset of daylight saving time - The DO Loop) used PG's approach.
Great stuff, Rick! Thanks
Here is a published sample that accounts for before and after 2007:
Sample 24735: Compute daylight saving time
http://support.sas.com/kb/24735
Cheers, Jan
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.