DATA Step, Macro, Functions and more

exploring code to calculate working days

Accepted Solution Solved
Reply
Contributor
Posts: 69
Accepted Solution

exploring code to calculate working days

Hey, 

I am exploring for code to calculate the the total no of working days in a month excluding sundays only.

i.e start date should be 1st date of every month and the end date should be last day of every month and calculate net working days in the current month. As the code should fetch the current month and calculate accordingly.

 

Thanks


Accepted Solutions
Solution
‎04-03-2018 08:29 AM
Super User
Super User
Posts: 9,421

Re: exploring code to calculate working days

Something like:

data want;
  do i=intnx('month',today(),0,'beginning') to intnx('month',today(),0,'end');
    if weekday(i) ne 1 then tot=sum(tot,1);
  end;
run;

View solution in original post


All Replies
Super User
Posts: 9,903

Re: exploring code to calculate working days

[ Edited ]

Remove all days where the weekday() function returns Saturday or Sunday.

Remove all days that can somehow be detected with the SAS holiday() function.

Remove all days that are country-specific or otherwise happen on a fixed date (May 1st, Christmas, New Year).

 

How you do it depends on how you need to use the result in further code.

For instance, you could create a table for a whole year (or a given timespan), and then create a custom format off that.

You could then create a macro

%macro num_workdays(month,year);
%local i;
%let i = 0;
%do day = %sysfunc(mdy(&month.,1,&year.)) %to %sysfunc(intnx(month,%sysfunc(mdy(&month.,1,&year.)),0,e));
  %if %sysfunc(putn(&day.,myholiday.)) %then %let i = %eval(&i. + 1);
%end;
&i.
%mend;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎04-03-2018 08:29 AM
Super User
Super User
Posts: 9,421

Re: exploring code to calculate working days

Something like:

data want;
  do i=intnx('month',today(),0,'beginning') to intnx('month',today(),0,'end');
    if weekday(i) ne 1 then tot=sum(tot,1);
  end;
run;
Contributor
Posts: 69

Re: exploring code to calculate working days

Thanks ... worked.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 101 views
  • 0 likes
  • 3 in conversation