Previous Page | Next Page

Functions and CALL Routines

ALLCOMB Function

Generates all combinations of the values of n variables taken k at a time in a minimal change order.
Category: Combinatorial
Restriction: The ALLCOMB function cannot be executed when you use the %SYSFUNC macro.

See Also


ALLCOMB(count, k, variable-1, ... , variable-n)



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


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


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 executing the ALLCOMB function.
Restriction: Specify no more than 33 items. If you need to find combinations of more than 33 items, use the CALL ALLCOMBI routine.
Tip: After executing ALLCOMB, the first k variables contain the values in one combination.


Use the ALLCOMB function in a loop where the first argument to ALLCOMB accepts each integral value from 1 to the number of combinations, and where k is constant. The number of combinations can be computed by using the COMB function. On the first execution, the argument types and lengths are checked for consistency. On each subsequent execution, the values of two variables are interchanged.

For the ALLCOMB function, the following actions occur:

If you execute the ALLCOMB function with the first argument out of sequence, the results are not useful. In particular, if you initialize the variables and then immediately execute the ALLCOMB function with a first argument of j, then you will not get the jth combination (except when j is 1). To get the jth combination, you must execute ALLCOMB j times, with the first argument taking values from 1 through j in that exact order.


SAS provides four functions or CALL routines for generating combinations:

ALLCOMBI is the fastest of these functions and CALL routines. LEXCOMB is the slowest.


The following is an example of the ALLCOMB function.

data _null_;
   array x[5] $3 ('ant' 'bee' 'cat' 'dog' 'ewe');
   do j=1 to ncomb+1;
      rc=allcomb(j, k, of x[*]);
      put j 5. +3 x1-x3 +3 rc=;

SAS writes the following output to the log:

    1   ant bee cat    rc=0
    2   ant bee ewe    rc=3
    3   ant bee dog    rc=3
    4   ant cat dog    rc=2
    5   ant cat ewe    rc=3
    6   ant dog ewe    rc=2
    7   bee dog ewe    rc=1
    8   bee dog cat    rc=3
    9   bee ewe cat    rc=2
   10   dog ewe cat    rc=1
   11   dog ewe cat    rc=-1

See Also

Functions and CALL Routines:


Previous Page | Next Page | Top of Page