Here's an example using sample data that might lead in the right direction:
*Getting data to simplify example...;
proc means nway missing noprint data=sashelp.prdsal2;
class state prodtype monyr;
var actual;
output out=sales(drop=_type_ _freq_ prodtype) sum=;
where prodtype='OFFICE';
run;
*Set end of current month.;
%let curr_month = '31DEC1995'd;
data _null_;
curr_month = &curr_month;
beg_month = "'"||put(intnx("month",&curr_month.,-6,"e"),date9.)||"'d";
call symput("beg_month",beg_month);
run;
*Limit sales to last 6 months.;
data have(where=(end_of_month between &beg_month. and &curr_month.));
set sales;
format end_of_month date9.;
end_of_month = intnx("month",monyr,0,"e");
if mod(_n_,5)=2 then delete; *simulate missing data -- this example set has an observation for each state and month...;
run;
*Macro;
%macro loop(dsin=/*input dataset*/, dsoutpfx=/*output data prefix*/);
%do i=1 %to 6;
data &dsoutpfx._IN_&i. &dsoutpfx._OUT_&i.;
merge
&dsin.(where=(end_of_month = intnx("month",&curr_month.,-&i.,"e")) in=OLD)
&dsin.(where=(end_of_month = intnx("month",&curr_month.,-&i.+1,"e")) in=NEW);
by state;
if not(OLD and NEW);
if OLD and not NEW then output &dsoutpfx._OUT_&i.;
else if NEW and not OLD then output &dsoutpfx._IN_&i.;
run;
%end;
%mend;
%loop(dsin=have,dsoutpfx=outsets)
-unison
... View more