Hello! Hope all is well!
I've run the code below and did not get the correct result for 'Saturday'. For the period of Jan 1, 2022 to March 31, 2022 I only get 12 instead of 13. Does any one know what if there is an error with my code? thanks.
>%let startdate = 1Jan2022;
%let enddate = 31Mar2022;
data Weekdays;
Sunday = intck('weekday234567W',"&startdate"d,"&enddate"d);
Monday = intck('weekday134567W',"&startdate"d,"&enddate"d);
Tuesday = intck('weekday124567W',"&startdate"d,"&enddate"d);
Wednesday = intck('weekday123567W',"&startdate"d,"&enddate"d);
Thursday = intck('weekday123467W',"&startdate"d,"&enddate"d);
Friday = intck('weekday123457W',"&startdate"d,"&enddate"d);
Saturday = intck('weekday123456W',"&startdate"d,"&enddate"d);
put _all_;
run;
this is the data I got:
| Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday |
| 13 | 13 | 13 | 13 | 13 | 12 | 12 |
Just count how many Saturdays are between the startdate and enddate. This year, there are 12.
As @Kurt_Bremser points out the INTCK function counts between the dates, and Jan 1st was a Saturday in 2022 and hence is not counted
Returns the number of interval boundaries of a given kind that lie between two dates, times, or datetime values.
Hello @Eugenio211,
Wouldn't it be easier to use shifted week intervals?
data Weekdays;
Sunday = intck('week.1',"&startdate"d-1,"&enddate"d);
Monday = intck('week.2',"&startdate"d-1,"&enddate"d);
Tuesday = intck('week.3',"&startdate"d-1,"&enddate"d);
Wednesday = intck('week.4',"&startdate"d-1,"&enddate"d);
Thursday = intck('week.5',"&startdate"d-1,"&enddate"d);
Friday = intck('week.6',"&startdate"d-1,"&enddate"d);
Saturday = intck('week.7',"&startdate"d-1,"&enddate"d);
put (_all_)(=);
run;
With the inserted "-1" the interval boundary before the start date is taken into account.
Hi @FreelanceReinh, I like this solution.
Can you say a little more about the 'Week.1' - 'Week.7' arguments. I guess Week.1 is equivalent to weekday234567W. I can't find either in the documentation -- just curious how you know to use these.
Thank you,
Hi @SAShole,
Here's another documentation link: Incrementing Dates and Times by Using Multipliers and By Shifting Intervals. The subsequent section "Commonly Used Time Intervals" includes the example
|
WEEK.2 |
Weekly intervals starting on Mondays |
@SAShole wrote:
I guess Week.1 is equivalent to weekday234567W.
Yes, I think this is true, but it's also equivalent to the plain Week interval without a shift index. The Weekday interval can be a bit tricky to understand at times, so the Week.i intervals seemed more intuitive to me for Eugenio211's task.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.