BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
parmis
Fluorite | Level 6

Hello,

 

Can anyone help me with the following code, please.

I'm trying to count the number of each days for the current month. I have also tried Today() for the date, but it doesn't work.

data test;

set test;

d0 = intnx( 'month', '31jan2019'd, 0 ) - 1;

d1 = intnx( 'month', '1feb2019'd, 1 ) - 1;

 Sunday = intck( 'week.1', d0, d1 );

 Monday = intck( 'week.2', d0, d1 );

 Tuesday = intck( 'week.3', d0, d1 );

 Wedday = intck( 'week.4', d0, d1 );

 Thurday = intck( 'week.5', d0, d1 );

 Friday = intck( 'week.6', d0, d1 );

 Satday = intck( 'week.7', d0, d1 );

drop d0 d1;

run;

Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Correct me if I'm wrong ... it looks like you are trying to count the number of Sundays, number of Mondays, etc. in the current month.  

 

If that's the case, skip counting the first 28 days of the month.  You already know there are 4 of each weekday in that time period.  Just count from day 29 to the end of the month:

 

data want;

set have;

array days {7} Sunday Monday Tuesday Wedday Thurday Friday Satday;

do d = 1 to 7;

   days{d} = 4;

end;

do d = 28 + intnx('month', today(), 0, 'begin') to intnx('month', today(), 0, 'end');

   days{weekday(d)} + 1;

end;

drop d;

run;

 

This should even work for February, where SAS would skip the final DO loop.  (And you can always switch from today() to "&sysdate9"d)  But it is untested code.

View solution in original post

13 REPLIES 13
PaigeMiller
Diamond | Level 26

@parmis wrote:

but it doesn't work


What do you see that is wrong? What are the outputs? What do you expect to get?

--
Paige Miller
parmis
Fluorite | Level 6

The table returns no result. I would need to have 8 separate column: Date, Sunday, Monday, Tuesday...

I'd need to get the counts for each day in a month.

Date         Sunday   Monday   Tuesday

Jan2019   5             4              4

LinusH
Tourmaline | Level 20

Perhaps you could use INTNX instead?

Untested:

intnx('MONTH',"&Ssydate9."d,0,'END') - intnx('MONTH',"&Sysdate9."D,0,'BEGINNING) +1 
Data never sleeps
ballardw
Super User
numberofdaysinmonth = day(intnx('month',today(),0,'E'));

the last parameter 'E' says to get the end of a period, in this case the month.

 

 

Which code that you show did you expect to get the number of days in a month?

Astounding
PROC Star

Correct me if I'm wrong ... it looks like you are trying to count the number of Sundays, number of Mondays, etc. in the current month.  

 

If that's the case, skip counting the first 28 days of the month.  You already know there are 4 of each weekday in that time period.  Just count from day 29 to the end of the month:

 

data want;

set have;

array days {7} Sunday Monday Tuesday Wedday Thurday Friday Satday;

do d = 1 to 7;

   days{d} = 4;

end;

do d = 28 + intnx('month', today(), 0, 'begin') to intnx('month', today(), 0, 'end');

   days{weekday(d)} + 1;

end;

drop d;

run;

 

This should even work for February, where SAS would skip the final DO loop.  (And you can always switch from today() to "&sysdate9"d)  But it is untested code.

parmis
Fluorite | Level 6

thank you very much! this is exactly what I wanted. how can I keep the date along with other fields?

Astounding
PROC Star

This program doesn't drop any variables.  What date did you want to keep?

parmis
Fluorite | Level 6

In the table, I'd like to have the date as well, but I only have the days of the week with the counts.

Astounding
PROC Star

You will have to give an example then.  What date are you talking about?  Today's date?  A date that comes from the incoming data?  Somehow, "keep the date" triggers the same question ... which date do you want to keep?

parmis
Fluorite | Level 6

yes, I'd like to have today's date.

 

Date         Sunday   Monday   Tuesday ....

Jan2019   5             4              4

Astounding
PROC Star

You can add a statement to the DATA step:

 

date = today();

 

If speed is an issue, this would be slightly faster:

 

date = "&sysdate9"d;

 

However, if your program begins before midnight and ends after midnight, the results could be slightly different.

parmis
Fluorite | Level 6

Thanks again!

FreelanceReinh
Jade | Level 19

Hello @parmis,

 

You can also use the NWKDOM function for this:

data want;
do wd=1 to 7; /* 1=Sunday, 2=Monday, ... */
  n=4+(day(nwkdom(5,wd,month(today()),year(today())))>28);
  output;
end;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 13 replies
  • 3212 views
  • 0 likes
  • 6 in conversation