# LEXCOMBI Function

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

 Category: Combinatorial Restriction: The LEXCOMBI function cannot be executed when you use the %SYSFUNC macro.

## Syntax

LEXCOMBI(n, k, index-1, …, k)

### 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 combination that is returned. Indices are integers between 1 and n inclusive.

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

## Details

Before the first execution of the LEXCOMBI function, 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, execute the LEXCOMBI function in a loop that executes COMB(n,k) times.
In the LEXCOMBI function, the returned value indicates which, if any, indices changed. If index-1 through index-i did not change, but index-j did change, wherej=i+1, then LEXCOMBI returnsi. If LEXCOMBI is called after the last combinations in lexicographic order have been generated, then LEXCOMBI returns –1.

## Comparisons

The LEXCOMBI function generates all combinations of the indices of n objects taken k at a time in lexicographic order. The ALLCOMBI function generates all combinations of the indices of n objects taken k at a time in a minimum change order.

## Example

The following example uses the LEXCOMBI function to generate combinations of indices in lexicographic order.
```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);
do j=1 to ncomb+1;
rc=lexcombi(n, k, of i[*]);
do h=1 to k;
c[h]=x[i[h]];
end;
put @4 j= @10 'i= ' i[*] +3 'c= ' c[*] +3 rc=;
end;
run;```
SAS writes the following output to the log:
```   j=1   i= 1 2 3    c= ant bee cat    rc=1
j=2   i= 1 2 4    c= ant bee dog    rc=3
j=3   i= 1 2 5    c= ant bee ewe    rc=3
j=4   i= 1 3 4    c= ant cat dog    rc=2
j=5   i= 1 3 5    c= ant cat ewe    rc=3
j=6   i= 1 4 5    c= ant dog ewe    rc=2
j=7   i= 2 3 4    c= bee cat dog    rc=1
j=8   i= 2 3 5    c= bee cat ewe    rc=3
j=9   i= 2 4 5    c= bee dog ewe    rc=2
j=10  i= 3 4 5    c= cat dog ewe    rc=1
j=11  i= 3 4 5    c= cat dog ewe    rc=-1```