In SAS if I have a string or an Array like the following,
array x[4] $1 ('A' 'B' 'C' 'D');
I need to generate all "Unique" permutations of the elements like the following,
[ABCD]
[ABC]
[BCD]
[ACD]
[ABD]
[AB]
[AC]
[AD]
[BC]
[BD]
[CD]
[A]
[B]
[C]
[D]
Is there a function in SAS for generating all possible combinations of the array?
Assumption: I believe you are looking for combinations
and not permutations
, so the order does not matter, BA
and AB
are same thing.
Use call allcomb
subroutine and comb
function to find out the possible combinations.
Read more about allcomb
and comb
here
and here
In short -
call allcomb
subroutine gives the possible combinations out of n
elements when r
of them are taken andcomb
function gives you how many combinations it would be when out of n
elements r
of them are taken at a time.data test(keep=my_string);
length my_string $50.;
array a[4] $ ('A' 'B' 'C' 'D');
n = dim(a);
do k=1 to n;
do j=1 to comb(n,k);
call allcomb(j,k,of a[*]);
do i = 1 to k;
if i=1 then do; my_string="";counter=0;end;
counter=counter+1;
my_string=cat(compress(my_string),compress(a[i]));
if counter=k then output;
end;
end;
end;
run;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With