Previous Page | Next Page

Functions and CALL Routines

CALL LEXCOMB Routine



Generates all distinct combinations of the non-missing values of n variables taken k at a time in lexicographic order.
Category: Combinatorial
Interaction: When invoked by the %SYSCALL macro statement, CALL LEXCOMB removes the quotation marks from its arguments. For more information, see Using CALL Routines and the %SYSCALL Macro Statement.

Syntax
Arguments
Details
The Basics
Number of Combinations
CALL LEXCOMB Processing
Using the CALL LEXCOMB Routine with Macros
Comparisons
Examples
Example 1: Using CALL LEXCOMB in a DATA Step
Example 2: Using CALL LEXCOMB with Macros
See Also

Syntax

CALL LEXCOMB(count, k, variable-1, ..., variable-n);


Arguments

count

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

k

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

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 LEXCOMB routine.
Tip: After calling LEXCOMB, the first k variables contain the values in one combination.

Details


The Basics

Use the CALL LEXCOMB routine in a loop where the first argument to CALL LEXCOMB takes each integral value from 1 to the number of distinct combinations of the non-missing values of the variables. In each call to LEXCOMB within this loop, k should have the same value.


Number of Combinations

When all of the variables have non-missing, unequal values, then the number of combinations is COMB(n,k). If the number of variables that have missing values is m, and all the non-missing values are unequal, then LEXCOMB produces COMB(n-m,k) combinations because the missing values are omitted from the combinations.

When some of the variables have equal values, the exact number of combinations is difficult to compute. If you cannot compute the exact number of combinations, use the LEXCOMB function instead of the CALL LEXCOMB routine.


CALL LEXCOMB Processing

On the first call to the LEXCOMB routine, the following actions occur:

On subsequent calls, up to and including the last combination, the next distinct combination of the non-missing values is generated in lexicographic order.

If you call the LEXCOMB 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 LEXCOMB routine with a first argument of j, you will not get the jth combination (except when j is 1). To get the jth combination, you must call the LEXCOMB routine j times, with the first argument taking values from 1 through j in that exact order.


Using the CALL LEXCOMB Routine with Macros

You can call the LEXCOMB 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 LEXCOMB 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

The CALL LEXCOMB routine generates all distinct combinations of the non-missing values of n variables taken k at a time in lexicographic order. The CALL ALLCOMB routine generates all combinations of the values of n variables taken k at a time in a minimal change order.


Examples


Example 1: Using CALL LEXCOMB in a DATA Step

The following example calls the LEXCOMB routine to generate distinct combinations in lexicographic order.

data _null_;
   array x[5] $3 ('ant' 'bee' 'cat' 'dog' 'ewe');
   n=dim(x);
   k=3;
   ncomb=comb(n,k);
   do j=1 to ncomb;
      call lexcomb(j, k, of x[*]);
      put j 5. +3 x1-x3;
   end;
run;

SAS writes the following output to the log:

    1   ant bee cat
    2   ant bee dog
    3   ant bee ewe
    4   ant cat dog
    5   ant cat ewe
    6   ant dog ewe
    7   bee cat dog
    8   bee cat ewe
    9   bee dog ewe
   10   cat dog ewe


Example 2: Using CALL LEXCOMB with Macros

The following is an example of the CALL LEXCOMB 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 ncomb=%sysfunc(comb(5,&k));
      %do j=1 %to &ncomb;
         %syscall lexcomb(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(&ncomb+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 baboon          sysinfo=1
    5: baboon hippopotamus    sysinfo=2
    6: baboon zebra           sysinfo=2
    7: hippopotamus zebra     sysinfo=1
    8: hippopotamus zebra     sysinfo=-1


See Also

Functions and CALL Routines:

LEXCOMB Function

CALL ALLCOMB Routine

Previous Page | Next Page | Top of Page