maidenhan 发表于 2012-11-6 14:31 
你的思路和我最初的思路一样,可惜这个思路有漏洞的。用下面这个样例跑一下,您就会发现这个思路的问题。 ...
Yes, I think I overlook the original request. The combination needs to be considered in here. I still see a lot of solution proposed is so complicated. Here is a solution using 'by logic' to deal with each subjn block. Within each subjn a combination will produced exactly for dt blocks. To simplify the problem I left only relevant variables and remove any dt block with only one obs in there.
The trick part here is the number of dt blocks in each subjn in unknown before hand. I use the different sub branch to handle it. The problem calls different sub-output-routine according to the number of dt blocks in a subjn block. I only write for cases of 2, 3 dt blocks in a subjn block. It is easily to expand it.
data test;
input subjn dt x ;
cards;
1 7 22
1 7 23
1 12 24
1 12 25
2 7 22
2 7 23
2 12 24
2 12 25
2 12 26
2 14 24
2 14 25
;
run;
data test2;
do n=1 by 1 until (last.subjn);
set test end=end;
by subjn dt;
array d[4] subjn dt order x ;
array ss[100, 4] _temporary_;
array s[5] s1-s5;
array e[5] e1-e5;
if first.subjn then do;
order=0;
idx=0;
end;
order+1;
if first.dt then do;
idx+1;
s[idx]=order;
end;
if last.dt then do;
e[idx]=order;
end;
l2=dim(d);
do i=1 to l2;
ss[order,i]=d
;
end;
end;
group=0;
if idx=2 then link out2;
else if idx=3 then link out3;
*else if idx=4 then link out4;
*else if idx=5 then link out5;
return;
out3:
do i=s1 to e1;
do j=s2 to e2;
do k=s3 to e3;
group+1;
do l=1 to l2;
d[l]=ss[i,l];
end;
output;
do l=1 to l2;
d[l]=ss[j,l];
end;
output;
do l=1 to l2;
d[l]=ss[k,l];
end;
output;
end;
end;
end;
return;
out2:
do i=s1 to e1;
do j=s2 to e2;
group+1;
do l=1 to l2;
d[l]=ss[i,l];
end;
output;
do l=1 to l2;
d[l]=ss[j,l];
end;
output;
end;
end;
return;
drop s1 s2 s3 s4 s5 e1 e2 e3 e4 e5 idx l2 i j k l;
run;
proc print;run;