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

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!

1. Peter Lancashire
Posted March 8, 2012 at 9:22 am | Permalink

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 .

• Posted March 8, 2012 at 9:27 am | Permalink

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!

• Clément
Posted September 12, 2014 at 6:07 am | Permalink

/*Compute the beginning and end of daylight saving time for several years
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;

2. Posted March 8, 2012 at 10:39 am | Permalink

Thanks for the reminder. :-)

3. Posted March 8, 2012 at 3:46 pm | Permalink

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.

4. Posted March 8, 2012 at 4:31 pm | Permalink

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.

• Posted March 8, 2012 at 7:42 pm | Permalink

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.

5. Doug Cole
Posted March 13, 2012 at 3:27 pm | Permalink

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;

6. WB
Posted August 6, 2012 at 2:18 pm | Permalink

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"

1. [...] to calculate the UTC offset within your SAS programs. And you can also use SAS to plan ahead, and note exactly which future Sundays will be affected by DST. That is, until the DST debate results in more changes. tags: time series data, utc Bookmark on [...]

2. [...] I was looking at some SAS documentation when I saw a Base SAS function that I never knew existed. The NWKDOM function returns the date for the nth occurrence of a weekday for the specified month and year. I surely could have used that function last spring when I blogged about how to compute the onset and end of daylight saving time (DST) in SAS! [...]

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of PROC IML and SAS/IML Studio. This blog focuses on statistical programming. It discusses statistical and computational algorithms, statistical graphics, simulation, efficiency, and data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.