DATA Step, Macro, Functions and more

How to group week wise with date range

Reply
Occasional Contributor
Posts: 13

How to group week wise with date range

Hi,

I have task date range please help me for the below scenorios.

 

i need to group with Sunday to Saturday  as Week1 in perticular month for example,

 

March 5th to March 11th it would be "Week1 March"  and March 12th To March 18th it would be "Week2 March"

 

 

 

Thanks,

Srinivas

 

 

 

Super Contributor
Posts: 345

Re: How to group week wise with date range

Posted in reply to srinivaschary

Please post an example data-set and the requested result dataset.

Super User
Posts: 7,760

Re: How to group week wise with date range

Posted in reply to srinivaschary

Use the weekday() function to determine Sundays, and then count the week in a retained variable:

data have;
do day_date = '01mar2017'd to '31mar2017'd;
  output;
end;
format day_date yymmddd10.;
run;

data want;
set have;
retain weekno 0;
length weektext $30;
if weekday(day_date) = 1 then weekno + 1;
if weekno > 0 then weektext = "Week" !! strip(put(weekno,2.)) !! " " !! strip(put(day_date,monname.));
drop weekno;
run;

proc print data=want noobs;
run;

Result:

  day_date     weektext

2017-03-01               
2017-03-02               
2017-03-03               
2017-03-04               
2017-03-05    Week1 March
2017-03-06    Week1 March
2017-03-07    Week1 March
2017-03-08    Week1 March
2017-03-09    Week1 March
2017-03-10    Week1 March
2017-03-11    Week1 March
2017-03-12    Week2 March
2017-03-13    Week2 March
2017-03-14    Week2 March
2017-03-15    Week2 March
2017-03-16    Week2 March
2017-03-17    Week2 March
2017-03-18    Week2 March
2017-03-19    Week3 March
2017-03-20    Week3 March
2017-03-21    Week3 March
2017-03-22    Week3 March
2017-03-23    Week3 March
2017-03-24    Week3 March
2017-03-25    Week3 March
2017-03-26    Week4 March
2017-03-27    Week4 March
2017-03-28    Week4 March
2017-03-29    Week4 March
2017-03-30    Week4 March
2017-03-31    Week4 March
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Trusted Advisor
Posts: 1,553

Re: How to group week wise with date range

Posted in reply to srinivaschary
Ask a Question
Discussion stats
  • 3 replies
  • 210 views
  • 0 likes
  • 4 in conversation