Solved
Posts: 3,040

# 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.

--
Paige Miller

Accepted Solutions
Solution
‎03-20-2018 11:58 AM
SAS Super FREQ
Posts: 4,242

All Replies
Posts: 5,532

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

Anywhere on Earth?

PG
Posts: 3,040

## 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.

--
Paige Miller
Posts: 5,532

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

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

PG

PG
Posts: 3,040

## 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

--
Paige Miller
Posts: 5,532

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

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
Solution
‎03-20-2018 11:58 AM
SAS Super FREQ
Posts: 4,242

Posts: 3,040

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

Great stuff, Rick! Thanks

--
Paige Miller
SAS Employee
Posts: 26

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

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

☑ This topic is solved.

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