# Generate All Unique Permutations of an Array in SAS

### Tags:

#### sas

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]
[BC]
[BD]
[CD]
[A]
[B]
[C]
[D]
``````

Is there a function in SAS for generating all possible combinations of the array?

305

#### babsdoc

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

• http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p0yx35py6pk47nn1vyrczffzrw25.htm

and here

• http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001009658.htm

In short -

• `call allcomb` subroutine gives the possible combinations out of `n` elements when `r` of them are taken and
• `comb` 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;
``````
200