SAS循环宏列表
您知道如何使用SAS和宏对列表进行迭代吗SAS循环宏列表,sas,Sas,您知道如何使用SAS和宏对列表进行迭代吗 %LET table = item1 item2;/*List of all input*/ /*I try to iterate on the list using a macro*/ %MACRO Main_Extract ; array orig[*] &table; do i=1 to dim(orig); %put orig[i]; end; %MEND; %Main_Ext
%LET table = item1 item2;/*List of all input*/
/*I try to iterate on the list using a macro*/
%MACRO Main_Extract ;
array orig[*] &table;
do i=1 to dim(orig);
%put orig[i];
end;
%MEND;
%Main_Extract;
如果表(项列表)是数组的变量名,则不需要宏。只需使用普通数据步骤代码,并使用宏变量列出数组元素
array orig &table;
do I = 1 to dim(orig);
put orig[I]=
end;
当宏变量包含以空格分隔的项列表时,通常通过在%do
循环中使用%scan
解析出每个项来使用宏内部的这些项。例如,为procsql语句生成一系列select子句
一次使用解析出每个项目
%macro special_sauce (items=);
%local i item;
%let i = 1;
%do %while (%length(%scan(&items,&i)));
%let item = %scan(&items,&i);
%put NOTE: code generated for &=item;
/* ... emit some SAS code or code-snippet involving &item ... */
&item._squared = &item ** 2; /* emit data step source statement that presumes item is a variable name that is being squared */
%let i = %eval(&i+1);
%end;
%mend;
options mprint;
data want;
set sashelp.class;
%special_sauce(items=age height)
run;
如果需要多次使用项目列表,将单个项目存储在本地宏变量中也很有帮助,以便于重复使用
多次使用的项目列表,分析一次并将项目放入“宏数组”。实际上没有宏数组这样的东西,只是可以迭代的数字后缀符号名的约定
%macro special_sauce2 (items=);
%local i item itemCount;
%let i = 1;
%do %while (%length(%scan(&items,&i)));
%let item = %scan(&items,&i);
%let itemCount = &i; /* track number of items parsed */
%local item&i; /* local macro variable name with numeric suffix, sometimes called a macro array */
%let item&i = &item; /* save the extracted item */
%let i = %eval(&i+1);
%end;
/* use the items in the 'macro-array' */
%do i = 1 %to &itemCount;
%put NOTE: value of macro variable item&i is &&item&i;
&&item&i.._2 = &&item&i ** 2;
%end;
/* use the items in the 'macro-array' */
%do i = 1 %to &itemCount;
%put NOTE: Hi again, value of macro variable item&i is &&item&i;
&&item&i.._3 = &&item&i ** 3;
%end;
%mend;
options mprint;
data want;
set sashelp.class;
%special_sauce2(items=age height)
run;
很好的经验法则,如果不需要,就不要使用宏。总体而言,您想做什么。这看起来像是数据步骤和宏代码的混合,可能无法实现您想要的功能