OK, let's give this a shot. This is untested code, so I hope it requires a minimum of debugging.
data want;
array addaweek {1998:2000, 53};
if _n_=1 then do until (done);
set _5_week_periods end=done;
addaweek{year, week}=1;
retain addaweek:;
end;
set have;
original_week_end = week_end;
do week_start = week_start to original_week_end by 4;
week_end = min(original_week_end, week_start + 3);
add1 = 0;
do week = week_start to week_end;
add1 + addaweek{year, week};
end;
if add1=0 then output;
else do;
week_end = min(original_week_end, week_end + 1);
output;
week_start = week_start + 1;
end;
end;
drop original_week_end add1 addaweek:;
run;
I think it works, but like I said it's untested. The idea is that the top DO loop loads the additional data set (the one that indicates the 5-week time periods) into an array. The rest of the DATA step breaks up the time periods into blocks of 4 (or 5) weeks. There are still loose ends such as what should happen when a block of 4 weeks ends in the middle of a 5-week month. But the code at least does make a decision about that.
See how close this comes to what you need. Good luck.
... View more