June 2012 Quick Tips

Quick Tip #1: The Holiday Function

Quick Tip #1: The Holiday Function


Have you ever had a program where you needed to use the date of a specific holiday within your processing?  Holidays such as New Year’s Day, Valentine’s Day, the 4th of July, or Christmas are easy to work with.  Every year, the new year begins on January 1st, Cupids’ arrows will be found on February 14th, and Santa will ride his sleigh around the world on December 25th.  What about many of the other holidays such as Mother’s Day, Father’s Day, Memorial Day, Labor Day, or Thanksgiving?  Each year these holidays fall on a different calendar date. 

When we reference one or more of these rotating holidays within our jobs we must be sure we are referencing the correct date.  Executing our job in 2012 we need to be sure that Thanksgiving is set as November 22nd and not as the 2011 date of November 24th.  Using these holidays within our programs requires one of the following:

·         Modifications to the program or a data file every year to reference the correct calendar date

·         Complex calculations to determine the correct date

Either of these methods is subject to problems.  When holiday date values are hard coded into our programs, we need to be sure we change the values prior to executing the job when the new dates are required.  Calculations for the dates may be more difficult for some holidays.  Mother’s Day may always fall on the second Sunday of May, but what calculation do you use for Easter?

When SAS 9.2 was released, a solution to the holiday dilemma was introduced.  The function HOLIDAY became available.  The dates of 22 different U.S. and Canadian holidays can be returned by this function.  Using this function SAS computes the date on which a specified holiday occurs in a specified year and returns the SAS date value for the holiday.


HOLIDAY (‘holiday’, year)

In your process if you need to identify Mother’s Day in the current year you could use the following statement:

Mothers_day_current = holiday('mothers', year("&sysdate9"d));

The result is the SAS date value of 19126, or 13MAY2012.

Now that a SAS date is available, you can apply any of the SAS date formats to display the date.  You may also use the date in further date calculations.  For example, you need to find all transactions which occurred on Black Friday.  While Black Friday is not a recognized holiday, once the date of Thanksgiving is known it can be calculated by adding 1 day to the date of Thanksgiving.

Black_friday = holiday('thanksgiving', year("&sysdate9"d)) + 1;