CALL ALLCOMBI Routine

Generates all combinations of the indices of n objects taken k at a time in a minimal change order.

Category: Combinatorial

Syntax

CALL ALLCOMBI(N, K, index-1, …, index-K, <, index-added, index-removed> );

Required Arguments

N

is a numeric constant, variable, or expression that specifies the total number of objects.

K

is a numeric constant, variable, or expression that specifies the number of objects in each combination.

index

is a numeric variable that contains indices of the objects in the returned combination. Indices are integers between 1 and N inclusive.

Tip If index-1 is missing or zero, then ALLCOMBI initializes the indices to index-1=1 through index-K=K. Otherwise, ALLCOMBI creates a new combination by removing one index from the combination and adding another index.

Optional Arguments

index-added

is a numeric variable in which ALLCOMBI returns the value of the index that was added.

index-removed

is a numeric variable in which ALLCOMBI returns the value of the index that was removed.

Details

CALL ALLCOMBI Processing

Before you make the first call to ALLCOMBI, complete one of the following tasks:
  • Set index-1 equal to zero or to a missing value.
  • Initialize index-1 through index-K to distinct integers between 1 and N inclusive.
The number of combinations of N objects taken K at a time can be computed as COMB(N, K). To generate all combinations of N objects taken K at a time, call ALLCOMBI in a loop that executes COMB(N, K) times.

Using the CALL ALLCOMBI Routine with Macros

If you call ALLCOMBI from the macro processor with %SYSCALL, then you must initialize all arguments to numeric values. &SYSCALL reformats the values that are returned.
If an error occurs during the execution of the CALL ALLCOMBI 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 and &SYSINFO are set to zero.

Comparisons

The CALL ALLCOMBI routine generates all combinations of the indices of N objects taken K at a time in a minimal change 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 ALLCOMBI in a DATA Step

The following is an example of the CALL ALLCOMBI routine that is used in a DATA step.
data _null_;
   array x[5] $3 ('ant' 'bee' 'cat' 'dog' 'ewe');
   array c[3] $3;
   array i[3];
   n=dim(x);
   k=dim(i);
   i[1]=0;
   ncomb=comb(n,k);    /* The one extra call goes back */
   do j=1 to ncomb+1;  /* to the first combination. */
      call allcombi(n, k, of i[*], add, remove);
      do h=1 to k;
         c[h]=x[i[h]];
      end;
      put @4 j= @10 'i= ' i[*] +3 'c= ' c[*] +3 add= remove=;
   end;
run;
SAS writes the following output to the log:
   j=1   i= 1 2 3    c= ant bee cat    add=0 remove=0
   j=2   i= 1 3 4    c= ant cat dog    add=4 remove=2
   j=3   i= 2 3 4    c= bee cat dog    add=2 remove=1
   j=4   i= 1 2 4    c= ant bee dog    add=1 remove=3
   j=5   i= 1 4 5    c= ant dog ewe    add=5 remove=2
   j=6   i= 2 4 5    c= bee dog ewe    add=2 remove=1
   j=7   i= 3 4 5    c= cat dog ewe    add=3 remove=2
   j=8   i= 1 3 5    c= ant cat ewe    add=1 remove=4
   j=9   i= 2 3 5    c= bee cat ewe    add=2 remove=1
   j=10  i= 1 2 5    c= ant bee ewe    add=1 remove=3
   j=11  i= 1 2 3    c= ant bee cat    add=3 remove=5

Example 2: Using CALL ALLCOMBI with Macros

The following is an example of the CALL ALLCOMBI routine that is used with macros.
%macro test;
   %let x1=0;
   %let x2=0;
   %let x3=0;
   %let add=0;
   %let remove=0;
   %let n=5;
   %let k=3;
   %let ncomb=%sysfunc(comb(&n,&k));
   %do j=1 %to &ncomb;
      %syscall allcombi(n,k,x1,x2,x3,add,remove);
      %let jfmt=%qsysfunc(putn(&j,5.));
      %put &jfmt: &x1 &x2 &x3 add=&add remove=&remove;
   %end;
%mend;
%test
SAS writes the following output to the log:
    1: 1 2 3 add=0 remove=0
    2: 1 3 4 add=4 remove=2
    3: 2 3 4 add=2 remove=1
    4: 1 2 4 add=1 remove=3
    5: 1 4 5 add=5 remove=2
    6: 2 4 5 add=2 remove=1
    7: 3 4 5 add=3 remove=2
    8: 1 3 5 add=1 remove=4
    9: 2 3 5 add=2 remove=1
   10: 1 2 5 add=1 remove=3

See Also

CALL Routines: