BookmarkSubscribeRSS Feed
sky299
Calcite | Level 5

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

2 REPLIES 2
Astounding
PROC Star

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!

cmepdx
Calcite | Level 5

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1228 views
  • 0 likes
  • 3 in conversation