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!
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 wrote:
but it doesn't work
What do you see that is wrong? What are the outputs? What do you expect to get?
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
Perhaps you could use INTNX instead?
Untested:
intnx('MONTH',"&Ssydate9."d,0,'END') - intnx('MONTH',"&Sysdate9."D,0,'BEGINNING) +1
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?
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.
thank you very much! this is exactly what I wanted. how can I keep the date along with other fields?
This program doesn't drop any variables. What date did you want to keep?
In the table, I'd like to have the date as well, but I only have the days of the week with the counts.
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?
yes, I'd like to have today's date.
Date Sunday Monday Tuesday ....
Jan2019 5 4 4
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.
Thanks again!
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!
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.