Generate all permutations of elements in SAS

Generate all permutations of elements in SAS

Permutation: Permutation can simply be defined as the several ways of arranging few or all members within a specific order. It is the process of legibly arranging from chaos. This is what is termed a Permutation.

To Generate all permutations of elements in SAS, use the <a href="https://documentation.sas.com/?cdcId=vdmmlcdc&cdcVersion=8.1&docsetId=lefunctionsref&docsetTarget=p17pfkttmfknqvn1wjr33oyz9yu6.htm&locale=en">FACT</a> function then determines the permutation using the CALL ALLPERM.

<a href="https://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=p0nqrwr48k3lsyn1rtv28qvr7feh.htm&docsetVersion=3.2&locale=en">CALL ALLPERM</a> and <a href="https://www.9to5sas.com/character-function-in-sas/">CATS</a> function are new in SAS 9.0.

data ex1;
  drop i perms x1-x3;
  array x (3) $3 ('a' 'b' 'c'); 
  length perm $3;
  perms=fact(3); 
    do i=1 to perms;   
      call allperm(i, of x(*));
      perm=cats(of x(*));
      output;
    end;
run;

Output:

Generate all permutations of elements in SAS

Combination: The combination is a process of selecting the objects or items from a set or the collection of objects, such that (unlike permutations) the order of selection of objects does not matter. It refers to the combination of N things taken from a group of K at a time without repetition.

data test(drop=x1-x3 h1-h3);
	array x[3] $3 ('a' 'b' 'c');
	array h[3] $3;
	n=dim(x);

	do k = 1 to 3;
		ncomb=comb(n,k);
		call sortc(of x(*));
		call missing(of h(*));

		do j=1 to ncomb;
			rc=allcomb(j,k,of x[*]);

			if rc<0 then
				leave;

			do i= 1 to k;
				h[i]=x[i];
			end;

			allcombns=cats(of h(*));
			output;
		end;
	end;

	drop i j k n ncomb rc;
run;

Output:

Generate all permutations of elements in SAS

 

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.