SAS Loop over a list of variables inside a macro (read every time)
I will need to loop over a list of variables inside a macro.
The list is created like this (I started the name of the variables 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 of them ... this macro is in the next 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 method does not work, because it transfers all the variables at the same time . The important thing is that if I want a loop, it must stay inside another datastep .....
I know this should be easy, I couldn't figure it out.
Thank!!!!
+1
source to share
2 answers
The best way to do this is to create your own proc sql
step to create all these macros.
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;
Creates a list of% addTendency () calls that you call referring to & trendList (which I named, but you may call it differently):
data labimp.muestra1;
set labimp.muestra1;
counter + 1;
by nnumero_de_cliente;
if first.nnumero_de_cliente then counter = 1;
&tendencyList.
run;
+1
source to share