问题
I would need to do a loop over a list of variables inside a macro.
The list is created in the following way (I have started the name of the variables that I want with MO, nu or KA):
proc sql noprint;
select name into :varsi separated by ' '
from dictionary.columns
where libname eq 'LABIMP' and memname eq 'MUESTRA1'
and (NAME LIKE 'MO_%' OR NAME LIKE 'nu_%' or name like 'KA_%');
quit;
Then, I need to run a macro for each one... this macro is inside the following data step:
data labimp.muestra1;
set labimp.muestra1;
counter + 1;
by nnumero_de_cliente;
if first.nnumero_de_cliente then counter = 1;
%addTendency(&varsi);
run;
Of course this way is not working because it brings all the variables at the same time. It's important that if I need a loop must remain inside the other datastep.....
I know it should be easy by I couldn't figure it out.
Thanks!!!!
回答1:
The best way to do this is to design your proc sql
step to create all of those macro calls.
proc sql ;
select cats('%addTendency(',name,')'
into :tendencyList separated by ' '
from dictionary.columns
where libname eq 'LABIMP' and memname eq 'MUESTRA1'
and (NAME LIKE 'MO_%' OR NAME LIKE 'nu_%' or name like 'KA_%');
quit;
That creates a list of %addTendency() calls that you then call by referencing &tendencyList (which I named, but you can name otherwise):
data labimp.muestra1;
set labimp.muestra1;
counter + 1;
by nnumero_de_cliente;
if first.nnumero_de_cliente then counter = 1;
&tendencyList.
run;
回答2:
You could do something like the following:
%do i=1 %to %sysfunc(countw(&varsi));
%addTendency(%scan(&varsi, &i));
%end;
来源:https://stackoverflow.com/questions/22722069/sas-loop-over-a-list-of-variables-inside-a-macro-read-one-each-time