SAS Loop over a list of variables inside a macro (read one each time)

隐身守侯 提交于 2019-12-13 18:42:00

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!