DATA Step, Macro, Functions and more

Code/algorithm to determine if a date/time was during Daylight Savings Time

Reply
Trusted Advisor
Posts: 1,918

Code/algorithm to determine if a date/time was during Daylight Savings Time


Anybody know of SAS code, or mathematical algorithm, that can determine if a date/time value was during Daylight Savings Time or not?

Thanks.

Respected Advisor
Posts: 4,923

Re: Code/algorithm to determine if a date/time was during Daylight Savings Time

Posted in reply to PaigeMiller

Anywhere on Earth? Smiley Wink

PG
Trusted Advisor
Posts: 1,918

Re: Code/algorithm to determine if a date/time was during Daylight Savings Time

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.

Respected Advisor
Posts: 4,923

Re: Code/algorithm to determine if a date/time was during Daylight Savings Time

Posted in reply to PaigeMiller

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

PG
Trusted Advisor
Posts: 1,918

Re: Code/algorithm to determine if a date/time was during Daylight Savings Time

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

Respected Advisor
Posts: 4,923

Re: Code/algorithm to determine if a date/time was during Daylight Savings Time

Posted in reply to PaigeMiller

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

PG
SAS Super FREQ
Posts: 3,753

Re: Code/algorithm to determine if a date/time was during Daylight Savings Time

Posted in reply to PaigeMiller
Trusted Advisor
Posts: 1,918

Re: Code/algorithm to determine if a date/time was during Daylight Savings Time

Great stuff, Rick! Thanks

SAS Employee
Posts: 26

Re: Code/algorithm to determine if a date/time was during Daylight Savings Time

Posted in reply to PaigeMiller

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

Ask a Question
Discussion stats
  • 8 replies
  • 1275 views
  • 0 likes
  • 4 in conversation