BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Amelia6
Obsidian | Level 7

I'd like to create a date variable that is based on the number of trading days from an event date. For instance, the event date is 1/2/2024, I'd like to create a date for 100 trading days from 1/5/2025. Roughly speaking, trading days are week days plus the following holidays:

 

  • Third Monday of January — Martin Luther King Jr. Day
  • Third Monday of February — Presidents' Day
  • Last Monday of May — Memorial Day
  • June 19 — Juneteenth
  • July 4 — Independence Day
  • First Monday of September — Labor Day
  • Fourth Thursday of November— Thanksgiving Day
  • Dec. 25 — Christmas Day

I understand I can do newdate=intnx('weekdays',olddate,100) to count only week days. But how can I also account for(exclude) the above holidays? Thank you! Attached is sample of data with old dates that I'd like to calculate the new dates on. Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
whymath
Lapis Lazuli | Level 10

You can just count:

data want;
  set raw.dates;

  _count_=0;
  do _n_=1 to 200 until(_count_=100);
    newdate=olddate+_n_;
    _year_=year(newdate);

    if not(
       weekday(newdate) in (1 7) /*Saturday,Sunday*/
       or newdate=holiday('mlk',_year_)
       or newdate=holiday('uspresidents',_year_)
       or newdate=holiday('memorial',_year_)
       or newdate=holiday('juneteenth',_year_)
       or newdate=holiday('usindependence',_year_)
       or newdate=holiday('labor',_year_)
       or newdate=holiday('thanksgiving',_year_)
       or newdate=holiday('christmas',_year_)
    ) then _count_+1;
  end;

  put olddate=  newdate= yymmdd10.;
run;

View solution in original post

4 REPLIES 4
whymath
Lapis Lazuli | Level 10

You can just count:

data want;
  set raw.dates;

  _count_=0;
  do _n_=1 to 200 until(_count_=100);
    newdate=olddate+_n_;
    _year_=year(newdate);

    if not(
       weekday(newdate) in (1 7) /*Saturday,Sunday*/
       or newdate=holiday('mlk',_year_)
       or newdate=holiday('uspresidents',_year_)
       or newdate=holiday('memorial',_year_)
       or newdate=holiday('juneteenth',_year_)
       or newdate=holiday('usindependence',_year_)
       or newdate=holiday('labor',_year_)
       or newdate=holiday('thanksgiving',_year_)
       or newdate=holiday('christmas',_year_)
    ) then _count_+1;
  end;

  put olddate=  newdate= yymmdd10.;
run;
Amelia6
Obsidian | Level 7

Thank you! It works beautifully! 

Ksharp
Super User
libname x v9 'c:\temp';
data want;
 set x.dates;
 array x{8};
 count=0;
 do newdate=olddate to '1jan2050'd;
/*  x1=holiday('MLK', year(newdate));*/
  x1=nwkdom(3,2,1, year(newdate));   /*Third Monday of January — Martin Luther King Jr. Day*/
  x2=nwkdom(3,2,2, year(newdate));  /*Third Monday of February — Presidents' Day*/
  x3=nwkdom(5,2,5, year(newdate));  /*Last Monday of May — Memorial Day*/
  x4=mdy(6,19, year(newdate));  /*June 19 — Juneteenth*/
  x5=mdy(7,4, year(newdate));  /*July 4 — Independence Day*/
  x6=nwkdom(1,2,9, year(newdate));  /*First Monday of September — Labor Day*/
  x7=nwkdom(4,5,11, year(newdate));  /*Fourth Thursday of November— Thanksgiving Day*/
  x8=mdy(12,25, year(newdate));  /*Dec. 25 — Christmas Day*/


  if weekday(newdate) not in (1 7) and newdate not in x then count+1;
  if count=100 then leave;
 end;
 format newdate x: yymmdd10.;
 drop count;
run;
Amelia6
Obsidian | Level 7

Thank you! It works wonders!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1186 views
  • 0 likes
  • 3 in conversation