CALL LEXPERK Routine

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.

Syntax

CALL LEXPERK(count, k, variable-1, …, variable-n);

Required Arguments

count

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

k

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

variable

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.

Details

The Basics

Use the CALL LEXPERK routine in a loop where the first argument to CALL LEXPERK accepts each integral value from 1 to the number of distinct permutations of k nonmissing values of the variables. In each call to LEXPERK within this loop, k should have the same value.

Number of Permutations

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.

CALL LEXPERK Processing

On the first call to the LEXPERK routine, the following actions occur:
  • The argument types and lengths are checked for consistency.
  • The m missing values are assigned to the last m arguments.
  • The n-m nonmissing values are assigned in ascending order to the first n-m arguments following count.
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 thejth permutation (except when j is 1). To get the jth permutation, you must call LEXPERK j times, with the first argument taking values from 1 through j in that exact order.

Using the CALL LEXPERK Routine with Macros

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:
  • &SYSERR is assigned a value that is greater than 4.
  • &SYSINFO is assigned a value that is less than –100.
If there are no errors, then &SYSERR is set to zero, and &SYSINFO is set to one of the following values:
  • 1 if count=1 and at least one variable has a nonmissing value
  • 1 if count>1 and the value of variable-1 changed
  • j if count>1 and variable-1 through variable-i did not change, but variable-j did change, where j=i+1
  • –1 if all distinct permutations were already generated

Comparisons

The CALL LEXPERK routine generates all distinct permutations of the nonmissing values of n variables taken k at a time in lexicographic order. The CALL ALLPERM routine generates all permutations of the values of several variables in a minimal change order.

Examples

Example 1: Using CALL LEXPERK in a DATA Step

The following is an example of the CALL LEXPERK routine.
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;
SAS writes the following output to the log:
    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

Example 2: Using CALL LEXPERK with Macros

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 
SAS writes the following output to the log:
    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