我上具有以下結構的數據集工作:SAS MACRO循環
Color Apple Orange Grape Avocado Blueberry
Yellow 1 . . . .
Orange . 1 . . .
Purple . . 1 . 1
我想寫每個果型創建表,選擇所有的顏色(行)的宏有一個值爲1.例如,蘋果TBL_APPLE的表格將有如下4行:
目前我正在考慮循環遍歷行和列。作爲弗里斯特一步,我打開所有的行和列變量引入宏:
/*rows*/
proc sql noprint;
select count(*) into :Nobs
from work.fruit;
select Color into :Attr1-:Attr%left(&Nobs)
from work.fruit;quit;
/*columns*/
proc contents data=work.fruit out=contents noprint; run;
%let n=&sqlobs;
proc sql; select name into :fruit1-fruit%left(&n) from contents; quit;
%macro fruit;
%do i=1 %to &NObs;
%do j=1 %to &n;
proc sql;
create table tlb_&&fruit&j as
select *
from work.fruit
where &n = &n;
quit;
%end;
%end;
%mend fruit;
%fruit;