Previous Page | Next Page

Functions and CALL Routines

CALL LEXPERM Routine



Generates all distinct permutations of the non-missing 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.

Syntax
Arguments
Details
Determine the Number of Distinct Permutations
CALL LEXPERM Processing
Using the CALL LEXPERM Routine with Macros
Comparisons
Examples
Example 1: Using CALL LEXPERM in a DATA Step
Example 2: Using CALL LEXPERM with Macros
See Also

Syntax

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


Arguments

count

specifies a numeric variable that has an integer value that ranges from 1 to the number of permutations.

variable

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.

Details


Determine the Number of Distinct Permutations

These variables are defined for use in the equation that follows:

N

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

M

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

d

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

Ni

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:

[equation]


CALL LEXPERM Processing

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.

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

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

For count>P, CALL LEXPERM returns -1.

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.


Using the CALL LEXPERM Routine with Macros

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.

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

If there are no errors, then &SYSERR is set to zero, and &SYSINFO is set to one of the following values:


Comparisons

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.


Examples


Example 1: Using CALL LEXPERM in a DATA Step

The following example uses the DATA step to generate all distinct permutations of the non-missing 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;

SAS writes the following output to the log:

    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


Example 2: Using CALL LEXPERM with Macros

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;
     

SAS writes the following output to the log:

    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


See Also

Functions and CALL Routines:

LEXPERM Function

CALL ALLPERM Routine

LEXPERK Function

CALL RANPERK Routine

CALL RANPERM Routine

Previous Page | Next Page | Top of Page