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

Category: | Combinatorial |

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. |

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

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.

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.

The following is an
example of the LEXPERM function.

data _null_; array x[6] $1 ('X' 'Y' 'Z' ' ' 'Z' 'Y'); nfact=fact(dim(x)); 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; end; run;

SAS writes the following
output to the log:

nfact=720 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

Copyright © SAS Institute Inc. All rights reserved.