This is not how I would recommend structuring the code, there are better ways to solve this problem.
But as I had 15 minutes before leaving for a holiday weekend, and I was enthralled by the Italian, I took your code, simplified it a bit, and made some test data.
The below code I think does what you would want. You can see I pulled the ODS Excel statement outside of the loop, and made a few other changes.
You can play with this, or with this working example, someone else will write a better-designed macro solution for you. This is a common macro problem: read in a control dataset with a list of datasets, and print each dataset.
*make some test data;
data ADDEBITI ;
set sashelp.class ;
run ;
data POLIZZE ;
set sashelp.shoes ;
run ;
*control dataset with list of datasets to print;
data Riepilogo_righe_2 ;
TABELLA='ADDEBITI' ; DIREZIONE='class' ; output ;
TABELLA='POLIZZE' ; DIREZIONE='shoes' ; output ;
run ;
%macro multiple (DATA=,);
/* Conta quante tabelle ci sono */
proc sql noprint;
select count(*) into :ntot
from Riepilogo_righe_2;
quit;
ods excel file="%sysfunc(pathname(work))/foo.xlsx" ;
%do i = 1 %to &ntot.;
/* Prende il nome della tabella i-esima */
proc sql noprint;
select LEFT(trim(TABELLA)), LEFT(trim(DIREZIONE))
into :nome, :des
from Riepilogo_righe_2
where monotonic() = &i ;
quit;
%put &nome.;
%PUT &NTOT.;
%put &DES.;
%if %sysfunc(find(&nome, ADDEBITI, i)) > 0 %then %do;
%put Condition 1 is met;
/* Add your SAS statements here */
ods excel options(
sheet_name='Addebiti'
);
proc print data=&nome.;
run;
%end;
%else %if %sysfunc(find(&nome, POLIZZE, i)) > 0 %then %do;
%put Condition 2 is met;
/* Add different statements here */
ods excel options(
sheet_name='Polizze'
);
proc print data=&nome. ;
run;
%end;
%else %do;
%put No conditions were met;
%end;
%END;
ods _all_ close ;
%mend multiple;
%multiple()
... View more