Spring forward, fall back: Using SAS to compute the onset of daylight saving time

11

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!

Share.

About Author

Rick Wicklin

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of PROC IML and SAS/IML Studio. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

11 Comments

    • Rick Wicklin

      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;

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

    • Rick Wicklin

      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.

  2. Pingback: Making up for lost time (UTC vs. DST) - The SAS Dummy

  3. 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;

  4. 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"

  5. Pingback: Computing the onset and end of daylight saving time in SAS: The EASY way! - The DO Loop

Leave A Reply

Back to Top