貌似lexcomb不能处理缺失值,
用allcomb做了一个;
data a;
input x1$ x2$ x3$;
cards;
A B C
B C D
D E .
;
run;
data b(keep=obs comb);
set a;
length obs 8 comb $10;
array x(3) x1-x3;
n=dim(x);
obs=0;
do i=1 to n;
ncomb=comb(n,i);
do j=1 to ncomb;
call allcomb(j,i,of x(*));
comb="";
id=0;
do m=1 to i;
if x(m)="" then id+1;
comb=catx(',',comb,x(m));
end;
if id=0 then do;
obs+1;
output;
end;
end;
end;
run;
我用宏完全实现了需求,见下面的程序。其中b是最终结果,带后缀的b是临时结果。想法是对a的每条观测都重新整理,去掉空值的变量,生成新的数据集x。然后用x生成数组做排列组合。
data a;
input x1$ x2$ x3$;
cards;
A B C
B C D
D E .
;
run;
data _null_;
set a end=end;
array x _all_;
col=compress(dim(x));
call symput('col',col);
if end then do;
obs=compress(_n_);
call symput('obs',obs);
end;
run;
%put &obs;
%put &col;
%macro b;
%do n=1 %to &obs;
data _null_;
set a;
if _n_=&n.;
array x _character_;
array y[&col.] $8 y1-y&col.;
do i=1 to &col.;
if x[i]^='' then y[i]=vname(x[i]);
end;
ccc=catx(' ', of y[*]);
call execute ('data x;set a;if _n_=&n.;keep '||ccc||';run;');
run;
data _null_;
set x;
array x _all_;
dim=compress(dim(x));
call symput('dim',dim);
run;
%do k=1 %to &dim.;
data _null_;
ncomb=comb(&dim.,&k.)+1;
call symput('ncomb',ncomb);
run;
data b&k.;
set x;
array x _all_;
%do i=1 %to &ncomb.;
rc=lexcomb(&i.,&k.,of x[*]);
if rc>0 then do;
y=catx(',',of x1-x&k.);
output;
end;
%end;
drop rc;
run;
%end;
run;