BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PaigeMiller
Diamond | Level 26


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

Thanks.

--
Paige Miller
1 ACCEPTED SOLUTION
8 REPLIES 8
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
PGStats
Opal | Level 21

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
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
PGStats
Opal | Level 21

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
PaigeMiller
Diamond | Level 26

Great stuff, Rick! Thanks

--
Paige Miller
jwsquillace
SAS Employee

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 3610 views
  • 0 likes
  • 4 in conversation