* 출처 :
http://cafe.daum.net/statsas/B3m/12770 /
http://cafe.daum.net/statsas/B3m/12775
/*
"데이터셋의 모든 관측치들에 대한 모든 가능한 조합을 만드는 문제"인 것 같네요.
아주 오래전(?)에도 여기에서 비슷한 문제(5947)가 있었는데.. 그 문제 붙잡고 끙끙거리던 생각이 나길래 찾아봤습니다. ^^;
비슷하게 적용하면 되네요.. ^^
*/
data indata;
format x $4.;
input x;
cards;
x01
x02
x03
x04
x05
x06
x07
x08
x09
x10
;
run;
* 관측치수가 가변적이라고 할 때, 관측치와 2진수format설정..;
proc sql noprint;
select count(x),
compress("binary"||put(count(x),z2.)||".") into :c, :binfmt
from indata;
quit;
* 모든 경우의 수를 2진수로 변환하고 그로부터 '1'의 위치와 갯수로 조합 생성;
data outdata;
length x_combi $ 500;
do i=1 to 2**&c-1; * 모든 경우의 수만큼 반복;
bin_val=put(i,&binfmt); * 2진수 생성;
x_count=count(bin_val,'1');
x_combi="";
do j=1 to &c;
if substr(bin_val,j,1)='1' then do; * 1의 위치=관측치의 obs번호;
set indata point=j;
x_combi=catx(",",x_combi,x);
end;
end;
output;
end;
stop;
keep x_combi x_count;
run;
proc sort;by x_count x_combi;run;
proc print;run;
***********************************************;
* ALLCOMB 함수 사용(백승민);
***********************************************;
* 최소 순서 정렬 : ALLCOMB / 사전식 순서 정렬 : LEXCOMB
* 이진수의 화려한 프로그래밍에 고개가 절로 숙여지는군요..
V9.2에 ALLCOMB 함수가 새로 생겨서 구현해 보았습니다. 관측치 내의 순서가 정렬이 안되어서 좀 보기가 그렇지만.(배열로 재정렬하면 될듯)
data BACK;
format x1-X10 $4.;
input x1-X10;
cards;
x01
x02
x03
x04
x05
x06
x07
x08
x09
x10
;
run;
data BACK1;
SET BACK;
array x[10] $3 X1-X10;
array Y[10] $3 Y1-Y10;
n=dim(x);
DO k=1 TO 10;
ncomb=comb(n,k);
do j=1 to ncomb;
call ALLCOMB(j, k, of x[*]);
do z=1 to k;
Y[z] = X[z];
NEW = CATX(",",of Y[*]);
end;
output;
end;
END;
stop;
*KEEP K NEW;
run;
data BACK2;
array x[10] $3 ('x1' 'x2' 'x3' 'x4' 'x5' 'x6' 'x7' 'x8' 'x9' 'x10');
array Y[10] $3 ;
n=dim(x);
DO k=1 TO 10;
ncomb=comb(n,k);
do j=1 to ncomb;
call allcomb(j, k, of x[*]);
do z=1 to k;
Y[z] = X[z];
NEW = CATX(",",of Y[*]);
end;
output;
end;
END;
*KEEP K NEW;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.