ALLPERM Function

Generates all permutations of the values of several variables in a minimal change order.

Category: Combinatorial


ALLPERM(count, variable-1 <,variable-2 …> )

Required Arguments


specifies a variable with an integer value that ranges from 1 to the number of permutations.


specifies either all numeric variables, or all character variables that have the same length. The values of these variables are permuted.

Restriction Specify no more than 18 variables.
Requirement Initialize these variables before you execute the ALLPERM function.


The Basics

Use the ALLPERM function in a loop where the first argument to ALLPERM accepts each integral value from 1 to the number of permutations. On the first execution, the argument types and lengths are checked for consistency. On each subsequent execution, the values of two consecutive variables are interchanged.
Note: You can compute the number of permutations by using the PERM function. For more information, see the PERM Function. For the ALLPERM function, the following values are returned:
  • 0 if count=1
  • J if the values of variable-J and variable-K are interchanged, where K=J+1
  • –1 if count>N!
If you use the ALLPERM function and the first argument is out of sequence, the results are not useful. For example, if you initialize the variables and then immediately execute the ALLPERM function with a first argument of K, your result will not be the Kth permutation (except when K is 1). To get the Kth permutation, you must execute the ALLPERM function K times, with the first argument taking values from 1 through K in that exact order.
ALLPERM always produces N! permutations even if some of the variables have equal values or missing values. If you want to generate only the distinct permutations when there are equal values, or if you want to omit missing values from the permutations, use the LEXPERM function instead.
Note: The ALLPERM function cannot be executed when you use the %SYSFUNC macro.


SAS provides three functions or CALL routines for generating all permutations:
  • ALLPERM generates all possible permutations of the values, missing or non-missing, of several variables. Each permutation is formed from the previous permutation by interchanging two consecutive values.
  • LEXPERM generates all distinct permutations of the non-missing values of several variables. The permutations are generated in lexicographic order.
  • LEXPERK generates all distinct permutations of K of the non-missing values of N variables. The permutations are generated in lexicographic order.
ALLPERM is the fastest of these functions and CALL routines. LEXPERK is the slowest.


The following example generates permutations of given values by using the ALLPERM function.
data _null_;
   array x [4] $3 ('ant' 'bee' 'cat' 'dog'); 
   do i=1 to nfact+1;   
      change=allperm(i, of x[*]);
      put i 5. +2 change +2 x[*];
SAS writes the following output to the log:
    1  0   ant bee cat dog
    2  3   ant bee dog cat
    3  2   ant dog bee cat
    4  1   dog ant bee cat
    5  3   dog ant cat bee
    6  1   ant dog cat bee
    7  2   ant cat dog bee
    8  3   ant cat bee dog
    9  1   cat ant bee dog
   10  3   cat ant dog bee
   11  2   cat dog ant bee
   12  1   dog cat ant bee
   13  3   dog cat bee ant
   14  1   cat dog bee ant
   15  2   cat bee dog ant
   16  3   cat bee ant dog
   17  1   bee cat ant dog
   18  3   bee cat dog ant
   19  2   bee dog cat ant
   20  1   dog bee cat ant
   21  3   dog bee ant cat
   22  1   bee dog ant cat
   23  2   bee ant dog cat
   24  3   bee ant cat dog
   25  -1   bee ant cat dog