Generates all distinct permutations of the nonmissing values of n variables taken k at a time in lexicographic order.

Category: | Combinatorial |

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

specifies an integer variable that is assigned a value from 1 to the number of permutations in a loop.

specifies an integer constant, variable, or expression between 1 and n, inclusive, that specifies the number of items in each permutation.

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

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

Tip | After calling LEXPERK, the first k variables contain the values in one permutation. |

When all of the variables
have nonmissing, unequal values, the number of permutations is PERM(,k). If the number of variables that have missing
values is m, and all the nonmissing
values are unequal, CALL LEXPERK produces PERM(n-m,k) permutations because the missing values are
omitted from the permutations. When some of the variables have equal
values, the exact number of permutations is difficult to compute.
If you cannot compute the exact number of permutations, use the LEXPERK
function instead of the CALL LEXPERK routine.

On subsequent calls,
up to and including the last permutation, the next distinct permutation
of k nonmissing values is generated
in lexicographic order.

If you call the LEXPERK
routine with the first argument out of sequence, then the results
are not useful. In particular, if you initialize the variables and
then immediately call the LEXPERK routine with a first argument of j, you will not get thej^{th} permutation (except when j is 1). To get the j^{th} permutation, you must call LEXPERK j times, with the first argument taking values
from 1 through j in that exact
order.

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

If an error occurs during
the execution of the CALL LEXPERK routine, then both of the following
values are set:

data _null_; array x[5] $3 ('V' 'W' 'X' 'Y' 'Z'); n=dim(x); k=3; nperm=perm(n,k); do j=1 to nperm; call lexperk(j, k, of x[*]); put j 5. +3 x1-x3; end; run;

1 V W X 2 V W Y 3 V W Z 4 V X W 5 V X Y 6 V X Z 7 V Y W 8 V Y X 9 V Y Z 10 V Z W 11 V Z X 12 V Z Y 13 W V X 14 W V Y 15 W V Z 16 W X V 17 W X Y 18 W X Z 19 W Y V 20 W Y X 21 W Y Z 22 W Z V 23 W Z X 24 W Z Y 25 X V W 26 X V Y 27 X V Z 28 X W V 29 X W Y 30 X W Z 31 X Y V 32 X Y W 33 X Y Z 34 X Z V 35 X Z W 36 X Z Y 37 Y V W 38 Y V X 39 Y V Z 40 Y W V 41 Y W X 42 Y W Z 43 Y X V 44 Y X W 45 Y X Z 46 Y Z V 47 Y Z W 48 Y Z X 49 Z V W 50 Z V X 51 Z V Y 52 Z W V 53 Z W X 54 Z W Y 55 Z X V 56 Z X W 57 Z X Y 58 Z Y V 59 Z Y W 60 Z Y X

The following is an example of the CALL LEXPERK 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 x5=zebra; %let k=2; %let nperk=%sysfunc(perm(5,&k)); %do j=1 %to &nperk; %syscall lexperk(j, k, x1, x2, x3, x4, x5); %let jfmt=%qsysfunc(putn(&j,5.)); %let pad=%qsysfunc(repeat(%str(),20-%length(&x1 &x2))); %put &jfmt: &x1 &x2 &pad sysinfo=&sysinfo; %if &sysinfo<0 %then %let j=%eval(&nperk+1); %end; %mend; %test

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

Copyright © SAS Institute Inc. All rights reserved.