08-30-2017 10:59 PM
I am trying to get number of working days by using intck('weekday', start,end). But the following code and the result confused me:
days1=intck('WEEKDAY', '02jul2016'd , '05jul2016'd );
days2=intck('WEEKDAY', '04jul2016'd , '07jul2016'd );
put "days1=" days1 "days2=" days2;
The result is:
Days2 value is what I expected. But what I expected for days1 was 1 instead of 2, since '02jul2016'd is Saturday and i am only counting weekdays which is Mon-Fri. So acturally days1 is calculating # of days between '04jul2016'd and '05jul2016'd. So it should be 1 day.
How does intck really work?
08-31-2017 12:07 AM
data _null_; D1=intck('WEEKDAY', '05jul2016'd , '05jul2016'd ); putlog D1=; D2=intck('WEEKDAY', '04jul2016'd , '05jul2016'd ); putlog D2=; D3=intck('WEEKDAY', '03jul2016'd , '05jul2016'd ); putlog D3=; D4=intck('WEEKDAY', '02jul2016'd , '05jul2016'd ); putlog D4=; run;
D1=0 wed-wed same day
D2=1 tue-wed one day
D3=2 mon-wed two days
D4=2 sun-wed still 2 days
Is it clearer now?
08-31-2017 01:09 AM
Is there a better function I can using so that Sat and Sun can be considered two days and to get the result I wanted, eg, how to make intck('WEEKDAY', '02jul2016'd , '05jul2016'd ) =1?
08-31-2017 01:21 AM
Oh I think you want *work* days, not *week* days.
Maybe this will help https://communities.sas.com/t5/SAS-Procedures/INTCK-Holidays/td-p/48472
08-31-2017 05:32 PM
So, instead of considering a start day in the weekend as a friday, you want to consider it as a monday. You want to count the boundaries between weekdays as if weekends did not exist. One way of achieving that is with a custom function, like this:
proc fcmp outlib=sasuser.fcmp.dates; function weekdaydif(date1, date2); return (intck("WEEKDAY", date1, date2) - (weekday(date1) in (1,7))); endsub; run; options cmplib=sasuser.fcmp; data _null_; days1 = weekdaydif('02jul2016'd , '05jul2016'd); days2 = weekdaydif('04jul2016'd , '07jul2016'd); put days1= days2=; run;
play with it to see if it does what you want.
08-31-2017 12:11 AM
According to the documentation, intck with the WEEKDAY interval counts daily intervals with Friday-Saturday-Sunday counted as the same day. Thus the "weekdays" involved in the calculation of days1 are 1-2-3/4/5, where / indicates the counted boundaries.