BookmarkSubscribeRSS Feed

Calculating the 3rd weekend of the month using SAS date functions (NWKDOM and INTNX)

Started ‎12-22-2023 by
Modified ‎01-04-2024 by
Views 663

This article shows how you can use the INTNX function to easily determine the date of the 3rd weekend of each calendar month.

 

The business questions

 

SAS Skillbuilder and SAS Viya for Learners provide a SAS great offering for educators and students with free-of-charge SAS Viya software access and SAS e-learnings.

 

I have been teaching data science and SAS programming classes at different universities and business schools in Austria for many years. And I am a happy user of the above offering in my lectures for my students.

 

Every software system needs maintenance and the SAS learning environment undergoes maintenance once a month. The announcement of the maintenance on the website reads as follows:

 

SAS Viya for Learners may be unavailable every 3rd weekend of the month from Friday at 8pm ET to Sunday at 2pm ET while we apply enhancements.

 

(Comment: In case that the first of the month is a Sunday, there will be a weekend that contains the 3rd Saturday of a month (21st of the month) and the 4th Sunday of the month (22nd of the month), while the 3rd Sunday was already the weekend before (15th of the month). The response of SAS tech support is that the 3rd weekend in their schedule is always the weekend in which the 3rd Saturday of the month falls.)

 

In order to plan my lectures schedule months ahead with the head of studies, I want to be able to generate a list of blocked weekends automatically.

 

The solution

 

The following SAS code allows go generate a list of these weekends. You simply need to adopt the year values in the DO year = 2024 TO 2025 statement.

 

 

data Weekend_3rd;
 format third_Saturday next_Sunday date9.;

 do year = 2024 to 2025;
  do month = 1 to 12;

   third_Saturday = nwkdom(3,7,month,year); 
   next_Sunday = third_Saturday + 1;

   output;

  end;
 end;

 drop month year;
run;
proc print;
run;
 
  • The DO loop iterates over the selected years and the 12 calendar months.
  • The 3rd Saturday is determined using the NWKDOM function.
    • the first parameter specifies the n-th repetition of the required weekday
    • the second parameter specifies the day of the week (1 = Sunday, ... , 7 = Saturday) 
    • MONTH and YEAR specify the Year.Month

 

The resulting output lists the maintenance weekends per month. I use this list to plan my lecture schedule.

gsvolba_0-1703254585916.png

 

 

Alternate solution using the INTNX function

 

You can also use the INTNX function for this task. 

 

 

   YearMonth = mdy(month,1,year);
   third_Saturday = intnx('week.7',YearMonth-1,3);
   next_Sunday = third_Saturday + 1;

 

 

A date variable with the first day of each month is generated using the MDY function.

The 3rd Saturday is determined using the INTNX function.

  • The date (first of the month) is moved to the last day of the previous month using the YearMonth -1 calculation.
  • WEEK.7 tells the function to consider weeks to start with the 7th day of the week (= Saturday) the value "3" instructs the INTNX function to increase the date value to the start of the 3rd "week". (as we defined week.7 to the 3rd Saturday of the month).   

If your data contains the date value already in a DATE variable (and not in separate YEAR and MONTH variables) the INTNX function allows you to use this variable directly. For the NWKDOM function you need to calculate the YEAR and MONTH value using the YEAR() and MONTH() functions.

 

The INTNX function is a great tool to calculate specific date values in a very flexible way. See also my article 3 ways to consider movable holidays in SAS .

 

Credits

Many thanks to @BrunoMueller and @Tom for reminding me that the NWKDOM() function is a much more direct solution for this problem than the INTNX function. Using the INTCK and INTNX functions heavily for my many tasks in my time series data preparation projects over the last years, the NWKDOM function disappeared somewhat from my radar 😉 .

 

References

 

 

 

 

 

Comments
Tom

Wouldn't it be much easier to just call the NWKDOM() function?

third_Saturday = NWKDOM(3, 7, month, year);

Hi Tom, fully agree, thank you
(have to admit that I used the INTCK and INTNX functions so heavily for my time series preparation projects over the last years that the NWKDOM function disappeared from my radar 😉 )
A colleague pointed this also out just before Christmas.
I am still on vacation, give me 2 more days to enhance this.

Gerhard

Version history
Last update:
‎01-04-2024 05:08 AM
Updated by:
Contributors

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags