Generates all distinct permutations of the nonmissing values of several variables in lexicographic order.

Category: | Combinatorial |

Interaction: | When invoked by the %SYSCALL macro statement, CALL LEXPERM removes the quotation marks from its arguments. For more information, see Using CALL Routines and the %SYSCALL Macro Statement. |

specifies a numeric variable that has 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 by LEXPERM.

Requirement | Initialize these variables before you call the LEXPERM routine. |

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

If the CALL LEXPERM
routine 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 CALL 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 CALL LEXPERM
K times, with the first argument accepting values from 1 through K
in that exact order.

You can call the LEXPERM routine when you use the %SYSCALL macro.
In this case, the variable arguments
are not required to be the same length, but they must be the same
type. If %SYSCALL identifies an argument as numeric, then %SYSCALL
reformats the returned value.

The following example uses the
DATA step to generate all distinct permutations of the nonmissing
values of several variables in lexicographic order.

data _null_; array x[4] $3 ('ant' 'bee' 'cat' 'dog'); n=dim(x); nfact=fact(n); do i=1 to nfact; call lexperm(i, of x[*]); put i 5. +2 x[*]; end; run;

1 ant bee cat dog 2 ant bee dog cat 3 ant cat bee dog 4 ant cat dog bee 5 ant dog bee cat 6 ant dog cat bee 7 bee ant cat dog 8 bee ant dog cat 9 bee cat ant dog 10 bee cat dog ant 11 bee dog ant cat 12 bee dog cat ant 13 cat ant bee dog 14 cat ant dog bee 15 cat bee ant dog 16 cat bee dog ant 17 cat dog ant bee 18 cat dog bee ant 19 dog ant bee cat 20 dog ant cat bee 21 dog bee ant cat 22 dog bee cat ant 23 dog cat ant bee 24 dog cat bee ant

The following is an example of the CALL LEXPERM routine that is
used with macros. The output includes values for the %SYSINFO macro.

%macro test; %let x1=ant; %let x2=baboon; %let x3=baboon; %let x4=hippopotamus; %let n=4; %let nperm=%sysfunc(perm(4)); %do j=1 %to &nperm; %syscall lexperm(j,x1,x2,x3,x4); %let jfmt=%qsysfunc(putn(&j,5.)); %put &jfmt: &x1 &x2 &x3 &x4 sysinfo=&sysinfo; %if &sysinfo<0 %then %let j=%eval(&nperm+1); %end; %mend; %test;

1: ant baboon baboon hippopotamus sysinfo=1 2: ant baboon hippopotamus baboon sysinfo=3 3: ant hippopotamus baboon baboon sysinfo=2 4: baboon ant baboon hippopotamus sysinfo=1 5: baboon ant hippopotamus baboon sysinfo=3 6: baboon baboon ant hippopotamus sysinfo=2 7: baboon baboon hippopotamus ant sysinfo=3 8: baboon hippopotamus ant baboon sysinfo=2 9: baboon hippopotamus baboon ant sysinfo=3 10: hippopotamus ant baboon baboon sysinfo=1 11: hippopotamus baboon ant baboon sysinfo=2 12: hippopotamus baboon baboon ant sysinfo=3 13: hippopotamus baboon baboon ant sysinfo=-1

Copyright © SAS Institute Inc. All rights reserved.