调殇 发表于 2013-11-4 18:43 
谢谢pobel大神。我用您的代码可以完美解决之前描述的问题。想解决的问题其实是关联规则中最小置信度计算的 ...
以下代码应该能实现所有组合,我没有做很多的测试,楼主可以试一下能不能解决问题。
data all;
input ID $ T $ @@;
cards;
1 X1 1 X2 1 X5 2 X2 2 X4 3 X2 3 X3 4 X1 4 X2 4 X4 5 X1 5 X3
6 X2 6 X3 7 X1 7 X3 8 X1 8 X2 8 X3 8 X5 9 X1 9 X2 9 X3
;
*** Get list of IDs per T value;
proc sort data=all;
by t id;
run;
data idfmt;
set all;
by t id;
length label $200;
retain fmtname "idlist" type "c" label;
if first.t then label="*"||id;
else label=catx("*",label,id);
if last.t then label=cats(label,"*");
if last.t;
rename t=start;
drop id;
run;
proc format cntlin=idfmt;
run;
*** Macro;
%macro test(lib=work,inds=have,dsout=wanted);
*** Count number of T- variables;
proc sql noprint;
select max(compress(name," ","a")) into : varnum
from dictionary.columns
where libname=upcase("&lib") and upcase(memname)=upcase("&inds");
quit;
*** All possible combinations;
data allgrp;
format n 3.;
%do i=1 %to &varnum ;
%do j=1 %to &i;
do t&j=&j to &varnum;
%end;
%let condition=1;
%let sub=&i;
%do %while(&sub ge 2);
%let condition=&condition and t&sub>t%eval(&sub-1);
%let sub=%eval(&sub-1);
%end;
if &condition then do;
n=n(of t1-t&varnum);
output;
end;
%do j=1 %to &i;
end;
%end;
%end;
run;
*** Counting;
data _null_;
length code code1 code2 $3000;
set allgrp end=last;
if _n_=1 then call execute("data &dsout; set have;");
var=compress(cats("T",of t1-t&varnum,"_count"),".");
array t(&varnum);
code=cats("tmp=put(T",t1,",$idlist.);");
if n=1 then do;
code1=cats(var,"=count(tmp,'*')-1;");
end;
else do;
code1=cats(var,"=0;");
code2=cats("do a=1 to t",t1,"_count; if 1");
do i=2 to n;
code2= strip(code2)||" and index(put(T"||cats(t(i))||",$idlist.), catx(scan(tmp,a),'*','*'))";
end;
code2=strip(code2)||" then "||strip(var)||"+1; end;";
end;
call execute(strip(code));
call execute(strip(code1));
call execute(strip(code2));
if last then call execute("drop tmp a; run;");
run;
%mend;
*** Test;
data have;
input T1 $ T2 $ T3 $;
cards;
X1 X2 X3
X1 X2 X5
X3 X4 X5
;
options nomprint;
%test(dsout=wanted1)
data have;
input T1 $ T2 $ T3 $ T4 $ T5 $;
cards;
X1 X2 X3 X5 X2
X1 X2 X5 X4 X4
;
%test(dsout=wanted2)