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.

Syntax

Required Arguments

count

specifies an integer variable 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.

Restriction Specify no more than 33 items. If you need to find combinations of more than 33 items, use the CALL ALLCOMBI routine.
Requirement Initialize these variables before executing the ALLCOMB function.
Tip After executing ALLCOMB, the first k variables contain the values in one combination.

Details

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:
  • On the first execution, ALLCOMB returns 0.
  • If the values of variable-i and variable-j were interchanged, where i<j, then ALLCOMB returns i.
  • If no values were interchanged because all combinations were already generated, then ALLCOMB returns –1.
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.

Comparisons

SAS provides four functions or CALL routines for generating combinations:
  • ALLCOMB generates all possible combinations of the values, missing or nonmissing, of N variables. The values can be any numeric or character values. Each combination is formed from the previous combination by removing one value and inserting another value.
  • LEXCOMB generates all distinct combinations of the nonmissing values of several variables. The values can be any numeric or character values. The combinations are generated in lexicographic order.
  • ALLCOMBI generates all combinations of the indices of N items, where indices are integers from 1 to N. Each combination is formed from the previous combination by removing one index and inserting another index.
  • LEXCOMBI generates all combinations of the indices of N items, where indices are integers from 1 to N. The combinations are generated in lexicographic order.
ALLCOMBI is the fastest of these functions and CALL routines. LEXCOMB is the slowest.

Example

The following is an example of the ALLCOMB function.
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+1;
      rc=allcomb(j, k, of x[*]);
      put j 5. +3 x1-x3 +3 rc=;
   end;
run;
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

CALL Routines: