Hi Cathy
There are so many issues with the code you provide that I wouldn't know where to start.
I thought it might be more helpful to give you a code example which could serve you as starting point to code what you need.
HTH
Patrick
data have;
do var1=1 to 3;
do var2=1 to 2;
othervar=var1*var2;
output;
end;
end;
run;
/* create macro var with list of dataset names */
proc sql;
select distinct cats('DS_',var1,'_',var2) into :DSList separated by ' '
from have;
quit;
/* macro creating SAS code block to output data into selected target data sets */
%macro SelectDS;
%let i=1;
%do %while (%scan(&DSList,&i,' ') ne );
%let DSname=%scan(&DSList,&i,' ');
if var1=%scan(&DSname,2,'_') and var2=%scan(&DSname,3,'_') then output &DSname %str(;)
else
%let i=%eval(&i+1);
%end;
/* finish if... then... else... block and catch undefined cases */
output UndefinedCases;
%mend;
/* process source data and output into target data sets */
data &DSList UndefinedCases;
set have;
/* do some processing here */
/* ....................
.................... */
/* generate SAS statements for output to target data sets */
%SelectDS
run;