Date: Sat, 10 Nov 2007 10:03:54 -0500 "Howard Schreier " "SAS(r) Discussion" "Howard Schreier " Computing First/Last Weekday of the Month

Here's something I found while straying from the point of a recent question.

Given a SAS date, how can one compute the first and last weekdays of the month in which it falls? Here's what I devised:

data first_last; keep mm first last ; format mm monyy7. first last weekdate.; do y = 1998 to 2007; do m = 1 to 12; mm = mdy(m,15,y); last = intnx('weekday',intnx('month',mm, 0,'e'),0); first = intnx('weekday',intnx('weekday',intnx('month',mm,-1,'e'),0),1); output; end; end; run;

The formula for LAST is not too messy. The inner INTNX call provides the last day of the month, and the outer INTNX call shifts a Saturday or Sunday to the preceding Friday.

I cannot see a way to symmetrically implement that logic to find the first weekday. That's because WEEKDAY intervals merge weekend days with the *preceding* non-weekend day. So the formula I devised finds the last weekday of the previous month, then utilizes yet another INTNX call to advance that to the next weekday (being the first weekday of the given month). So it ends up with three nested INTNX calls. Is there a better (more concise) way?

Back to: Top of message | Previous page | Main SAS-L page