Help using Base SAS procedures

Question: How to use do loops for increment date values

Reply
N/A
Posts: 1

Question: How to use do loops for increment date values

May someone help me figure out how to use do loops to get the increment date values?

I need to make a data set contains five years worth of data for each holiday by using do loops.  The results should be:

New Year's Day   01/01/13 

New Year's Day   01/01/14 

New Year's Day   01/01/15 

New Year's Day   01/01/16 

New Year's Day    01/01/17 

Martin Luther King's Birthday  01/21/13 

Martin Luther King's Birthday  01/21/14 

Martin Luther King's Birthday  01/21/15

Martin Luther King's Birthday  01/21/16

Martin Luther King's Birthday  01/21/17

Datalines:

----+----1----+----2----+----3----+----4----+----5----+----6

01/01/13  New Year's Day

01/21/13  Martin Luther King's Birthday

02/02/13  Groundhog Day

02/12/13  Lincoln's
Birthday

02/14/13  Valentine's Day

02/18/13  President's Day and Washington's Birthday

03/17/13  St. Patrick's Day

03/29/13  Good Friday

04/01/13  April Fool's Day

04/01/13  Easter Monday

05/05/13  Cinco de Mayo

05/12/13  Mother's Day

05/27/13  Memorial Day

06/14/13  Flag Day

06/16/13  Father's Day

07/04/13  Independence Day

09/02/13  Labor Day

11/14/13  Columbus Day

10/31/13  Halloween

11/11/13  Veteran's Day

11/28/13  Thanksgiving

12/25/13  Christmas Day

12/31/13  New Year's Eve

Super User
Posts: 5,083

Re: Question: How to use do loops for increment date values

For some holidays it will be easy.  For example:

data holidays;

   length holiday $ 40;

   format date mmyydd10.;

   do year=2013 to 2017;

      holiday="Christmas Day";

      date = mdy(12, 25, year);

      output;

      holiday="New Year's Eve";

      date = mdy(12, 31, year);

      output;

   end;

run;

But many of the holidays that you mention will not fall on the same day of the year each time.  In fact, Good Friday is sometimes in March and sometimes in April.  You will first have to find the rules that determine the date for each holiday.  No programming is involved here ... just understanding the rules for what you are trying to create.

Once you know the rules, you will need to understand how SAS stores dates, and you will need to understand a few date-related functions (such as MDY illustrated here).  The calculations inside the DO loop that determine DATE will become more complex, using a combination of tools.  For example, Thanksgiving always falls on the fourth Thursday of November.  You might code something like this:

holiday="Thanksgiving";

begin_Nov = mdy(11, 1, year);

date = begin_Nov + (5 - weekday(Begin_Nov)) + 21;

if weekday(Begin_Nov) > 5 then date = date + 7;

So you have some research and some studying to do!

New Contributor
Posts: 3

Re: Question: How to use do loops for increment date values

Look into the "holiday" function. That will handle the logic for a good deal of what you have in your list, but not all.

Ask a Question
Discussion stats
  • 2 replies
  • 196 views
  • 0 likes
  • 3 in conversation