I want to generate all possible unique combinations for a series. For Example,
data have;
input combinations $1.;
cards;
A
B
C
D
E
;
run;
I want a column that has ( i think this works out to 32 distinct combinations, 2^5, no?)
A
AB
ABC
ABCDE
B
BC
......
Thank you,
So, what you want is all possible subsets :
data test(keep=k sub);
array II{5} (5*0);
length sub $5;
k = -1;
do i = 1 to 2**5;
call graycode (k, of II{*});
call missing(sub);
do j = 1 to 5;
if II{j} then sub = cats(sub,char("ABCDE",j));
end;
output;
end;
run;
PG
I think 2^5 -1
data _null_;
array a[5] $ ('A' 'B' 'C' 'D' 'E');
n = dim(a);
do k=1 to n;
do j=1 to comb(n,k);
counter+1;
call allcomb(j,k,of a
put counter 5. +3 @;
do i = 1 to k;
put a $5. @;
end;
put;
end;
end;
run;
Thanks Art!
Can you show me how to modify this code such that I can have a column with all the combinations?
Jeffrey,
Not sure what you are asking regarding one column. Possibly something like:
data want(keep=want);
array a[5] $ ('A' 'B' 'C' 'D' 'E');
length temp $5 want $500;
format want $500.;
n = dim(a);
want='';
do k=1 to n;
do j=1 to comb(n,k);
counter+1;
call allcomb(j,k,of a
temp='';
do i = 1 to k;
temp=catt(temp, a);
end;
want=catx(',',want,temp);
end;
end;
run;
Question: for example, will 'AB' be equivalent to 'BA'? in other words, will the order matter?
Haikuo
For this i'm treating AB as equivalent to BA. Order does not matter.
So, what you want is all possible subsets :
data test(keep=k sub);
array II{5} (5*0);
length sub $5;
k = -1;
do i = 1 to 2**5;
call graycode (k, of II{*});
call missing(sub);
do j = 1 to 5;
if II{j} then sub = cats(sub,char("ABCDE",j));
end;
output;
end;
run;
PG
I found an interesting way. Maybe you like it.
data have; input combinations $1.; cards; A B C D E ; run; proc transpose data=have out=x(keep=col:); var combinations; run; proc means data=x noprint; class _all_; output out=want(keep=col:); run; data want; set want; combination=cats(of col:); run;
Ksharp
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.