In the United States, this upcoming weekend is when we turn our clocks forward one hour as we adopt daylight saving time. (Some people will also flip their mattresses this weekend!)
Daylight saving time (DST) in the US begins on the second Sunday in March and ends on the first Sunday in November. The US Naval Observatory has a table that shows when daylight saving time begins and ends. But how can you create such a table and how can you find the onset of DST for future years? By writing a SAS program, of course!
Base SAS has a useful little function called the INTNX function that enables you to find a date that is a certain number of time intervals after an earlier date. For example, you can use the INTNX function to find the date that is 7 weeks after today's date. There is even a SAS Knowledge Base sample that shows how to use the INTNX function to compute the beginning and end of daylight saving time. The following program simplifies the sample:
/* compute the beginning and end of daylight saving time for several years */ data DST; format dst_beg dst_end DATE5.; do year=2012 to 2022; fdo_mar = mdy( 3,1,year); /* first day of March */ fdo_nov = mdy(11,1,year); /* first day of November */ /* DST begins on the second Sunday in March */ incr =(weekday(fdo_mar)^=1) + 1; /* 1 if fdo_mar is Sunday; 2 otherwise */ dst_beg=intnx("week",fdo_mar,incr); /* 1 or 2 weeks after 01MAR */ /* DST ends on the first Sunday in November */ incr =(weekday(fdo_nov)^=1); /* 0 if fdo_nov is Sunday; 1 otherwise */ dst_end=intnx("week",fdo_nov,incr); /* 0 or 1 weeks after 01NOV */ output; end; run; proc print noobs; var year dst_beg dst_end; run; |
The tricky part about this code is the definition of the incr variable. If the first day of March is a Sunday, incr is set to 1. If the first day of March is Monday through Saturday, incr is set to 2. In either case, the "week" interval causes the INTNX function to return the first day (Sunday) of the second week in March, which is the day that DST begins. The calculation of the first Sunday in November is handled similarly.
There are some people who oppose flipping the clocks back and forth twice a year, but the rest of us can run our SAS program and then "spring forward and fall back" on the correct day. Or is it "spring back and fall forward"? Those of us who can't remember can insert that information into the program as well!
11 Comments
Thanks for this tip. I think it is worth mentioning that other countries change their clocks according to different rules. In the European Union the clocks will change two weeks later. It also has a different name: summer time. There is some information here: http://en.wikipedia.org/wiki/Daylight_saving .
Good point. These rules are fairly recent for the US; we used different rules until 2007. Also, the state of Arizona and Arizona and Hawaii do not observe DST. Feel free to modify the program to fit your local customs!
This is your code adapted to DST in France (simpler)
/*Compute the beginning and end of daylight saving time for several years
(adapté à la France)*/
Data DST;
do year = 2014 to 2022;
ldo_mar = mdy(3,31,year); /*Last day of March*/
ldo_oct = mdy(10,31,Year); /*Last day of October*/
/*DST begins on the last sunday in March*/
dst_beg = intnx("week",ldo_mar,0);
/*Dst ends on the last Sunday in October*/
dst_end = intnx("week",ldo_oct,0); output;
end;
run;
Proc print data = Dst noobs label;
format dst_beg dst_end Date5.;
var Year dst_beg dst_end;
label Year = "Année" dst_beg = "Début" dst_end = "Fin";
run;
Thanks for the reminder. :-)
There are only fourteen possible 'years' (going by the day of the week the year begins, and if there is a leap year). So, it should be easy to set up a macro which returns the beginning and end of DST, given the year.
How and when are the DST rules updated in SAS? We still use SAS 9.1.3 and SAS 9.2, and there were changes to the US DST laws as recently as 2007.
There are no rules built into SAS. This program finds the second Sunday in March and the first Sunday in November. The program runs for any version of SAS that supports the INTNX function.
Pingback: Making up for lost time (UTC vs. DST) - The SAS Dummy
Or stated more directly:
data DST;
format dst_beg dst_end DATE5.;
do year = 2012 to 2022;
dst_beg = intnx ("week", mdy ( 3, 1, year), (weekday (mdy ( 3, 1, year)) ^= 1) + 1); /* 1 or 2 weeks after 01MAR */
dst_end = intnx ("week", mdy (11, 1, year), (weekday (mdy (11, 1, year)) ^= 1) ); /* 1 or 2 weeks after 01NOV */
output;
end;
run;
The program works but there's a mistake in one of the comments explanations.
The commented statement next to the second "incr =" statement should read "0 if fdo_nov is Sunday; 1 otherwise"
Pingback: Computing the onset and end of daylight saving time in SAS: The EASY way! - The DO Loop