BookmarkSubscribeRSS Feed
Eugenio211
Quartz | Level 8

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
6 REPLIES 6
AMSAS
SAS Super FREQ

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.

FreelanceReinh
Jade | Level 19

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.

SAShole
Pyrite | Level 9

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,

FreelanceReinh
Jade | Level 19

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.

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 4218 views
  • 8 likes
  • 6 in conversation