Previous Page | Next Page

Functions and CALL Routines

LEXPERM Function

Generates all distinct permutations of the non-missing values of several variables in lexicographic order.
Category: Combinatorial

Determine the Number of Distinct Permutations
LEXPERM Processing
See Also


LEXPERM(count, variable-1 <, ..., variable-N>)



specifies an integer variable 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 by LEXPERM.

Requirement: Initialize these variables before you execute the LEXPERM function.


Determine the Number of Distinct Permutations

The following variables are defined for use in the equation that follows:


specifies the number of variables that are being permuted--that is, the number of arguments minus one.


specifies the number of missing values among the variables that are being permuted.


specifies the number of distinct non-missing values among the arguments.


for i=1, through i=d, Ni specifies the number of instances of the ith distinct value.

The number of distinct permutations of non-missing values of the arguments is expressed as follows:


Note:   The LEXPERM function cannot be executed with the %SYSFUNC macro.  [cautionend]

LEXPERM Processing

Use the LEXPERM function in a loop where the argument count takes each integral value from 1 to P. You do not need to compute P provided you exit the loop when LEXPERM returns a value that is less than zero.

For 1=count<P, the following actions occur:

For 1<count<=P, the following actions occur:

For count>P, LEXPERM returns -1.

If the LEXPERM function is executed with the first argument out of sequence, the results might not be useful. In particular, if you initialize the variables and then immediately execute LEXPERM with a first argument of K, you will not get the Kth permutation (except when K is 1). To get the Kth permutation, you must execute LEXPERM K times, with the first argument accepting values from 1 through K in that exact order.


SAS provides three functions or CALL routines for generating all permutations:

ALLPERM is the fastest of these functions and CALL routines. LEXPERK is the slowest.


The following is an example of the LEXPERM function.

data _null_;
   array x[6] $1 ('X' 'Y' 'Z' ' ' 'Z' 'Y');
   put +3 nfact=;
   do i=1 to nfact;
      rc=lexperm(i, of x[*]);
      put i 5. +2 rc= +2 x[*];
      if rc<0 then leave;

SAS writes the following output to the log:

    1  rc=1   X Y Y Z Z  
    2  rc=3   X Y Z Y Z  
    3  rc=4   X Y Z Z Y  
    4  rc=2   X Z Y Y Z  
    5  rc=4   X Z Y Z Y  
    6  rc=3   X Z Z Y Y  
    7  rc=1   Y X Y Z Z  
    8  rc=3   Y X Z Y Z  
    9  rc=4   Y X Z Z Y  
   10  rc=2   Y Y X Z Z  
   11  rc=3   Y Y Z X Z  
   12  rc=4   Y Y Z Z X  
   13  rc=2   Y Z X Y Z  
   14  rc=4   Y Z X Z Y  
   15  rc=3   Y Z Y X Z  
   16  rc=4   Y Z Y Z X  
   17  rc=3   Y Z Z X Y  
   18  rc=4   Y Z Z Y X  
   19  rc=1   Z X Y Y Z  
   20  rc=4   Z X Y Z Y  
   21  rc=3   Z X Z Y Y  
   22  rc=2   Z Y X Y Z  
   23  rc=4   Z Y X Z Y  
   24  rc=3   Z Y Y X Z  
   25  rc=4   Z Y Y Z X  
   26  rc=3   Z Y Z X Y  
   27  rc=4   Z Y Z Y X  
   28  rc=2   Z Z X Y Y  
   29  rc=3   Z Z Y X Y  
   30  rc=4   Z Z Y Y X  
   31  rc=-1   Z Z Y Y X  

See Also

Functions and CALL Routines:


ALLPERM Function



Previous Page | Next Page | Top of Page