3月 082012
 

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);       /* 1 if fdo_nov is Sunday; 0 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!

tags: Just for Fun, SAS Programming

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)