1
votes

I'm new to SAS and have been trying to figure out how to get the week number for each month. I'm having an issue with the months where they don't start at the beginning of the week. For example, if I have a month where the data from the 1st of the month falls on a Thursday, it shows the 1st and 2nd of that month as week 0. Is there a way to display those weeks as week 1? I've tried different things and have been unsuccessful.

DATA getweek;
set test; 
if year(rundt) ne year(today())then delete;
   month = month(rundt); 
   week1=intck('week',intnx('month',rundt,0),rundt);
   format rundt MMDDYY8.;     
RUN;
1

1 Answers

0
votes

This depends largely on how you want to define week number. If the first of the month falls on a wednesday, what week is that week? Is that week one, or week zero? It can be commonly referred to either way.

If you want one, so Thurs/Fri/Sat are week 1, then the 4th is week 1, then just add one to the result - it's consistently going to be off (down) by one, after all.

data test;
do rundt=19805 to 19904;
day_of_month=day(rundt);
output;
end;
format rundt date9.;
run;

DATA getweek;
set test; 
if year(rundt) ne year(today())then delete;
   month = month(rundt); 
   week1=intck('week',intnx('month',rundt,0),rundt)+1;
   format rundt MMDDYY8.;     
RUN;

Now, if you want the first 9 or 10 days to be week 1, you have a different solution (though I don't recommend this). Use min to set it to minimum of one. This means your first week could be as many as 13 days, which is counterintuitive, but if it's what you need, this is how you do it.

DATA getweek;
set test; 
if year(rundt) ne year(today())then delete;
   month = month(rundt); 
   week1=min(intck('week',intnx('month',rundt,0),rundt),1);
   format rundt MMDDYY8.;     
RUN;