SAS Employee

## Juletip #15 - Listing all possible combinations of Santa's deers

/*****************************************************************************************

This code creates all possible combinations for a set of items.

Here we want to list all possible combinations of Santa's 8 reindeers to help Rudolf to
pull the sledge. Rudolf is always needed to share light with his nose.

Code utilises the binary representation of numbers, and the fact that between 9 digit
binary number 00000000 (i.e. 0) and 11111111 (i.e. 255) all possible combinations of 1's
and 0's are represented.

If the n'th digit of the binary string is 1, we concatenate the name of the n'th deer
into the resulting text. Here the resulting variable is named: Combination.

******************************************************************************************/
%let N = 8;
%let _DEERS = 'Dasher', 'Dancer', 'Prancer', 'Vixen', 'Comet', 'Cupid', 'Donner', 'Blitzen';

data _null_;
length Combinations \$ 240;
array Reindeers{&n.} \$ (&_DEERS.);

/* "_n_of_ones_" is an auxiliary variable containing the number of 1's in the 9 digit binary string. */
_n_of_ones_ = count(put( 0, binary&n..), "1");

/* We iterate the code until the whole binary string is full of 1's. */
do until(_n_of_ones_ eq dim(Reindeers));
_iteration_ + 1;
Combinations = "Rudolph";

/* Variable "i" loops through the 0's and 1's in the current binary string. */
do i = 1 to dim(Reindeers);

/* If current char in the binary string is "1", we concatenate the text into the resulting variable. */
if substr(put(_iteration_, binary&n..), i, 1) = "1"
then Combinations = catx(" + ", Combinations, Reindeers{i});
end;
put Combinations =;

/* We check how many 1's there is in the binary string in this iteration. */
_n_of_ones_ = count(put(_iteration_, binary&n..), "1");
end;
run;

3 REPLIES 3
SAS Super FREQ

## Re: Juletip #15 - Listing all possible combinations of Santa's deers

Hello,

Nice and fun thinking (it works!) , but instead of racking my brain , I would let SAS do all the work.

See here :

Generate combinations in SAS
By Rick Wicklin on The DO Loop September 30, 2013
https://blogs.sas.com/content/iml/2013/09/30/generate-combinations-in-sas.html

Also possible with PROC PLAN (SAS/STAT) and for sure also with SAS/OR (PROC CPM?).

BR,

Koen

Super User

## Re: Juletip #15 - Listing all possible combinations of Santa's deers

An alternate approach using the ALLCOMB function:

The Select is to pick which values to display from the array. The If j le ncomb is because the loop for J increments one additional time to, in effect reset the array and can result in duplicate output when displayed for the "ncomb+1" output.

```data _null_;
array x[8] \$ 10 ( 'Dasher', 'Dancer', 'Prancer', 'Vixen', 'Comet', 'Cupid', 'Donner', 'Blitzen');
n=dim(x);
do k=1 to dim(x);
ncomb=comb(n, k);
do j=1 to ncomb+1;
rc=allcomb(j, k, of x[*]);
if j le ncomb then  select (k);
when (1) put  x1;
when (2) put  x1-x2;
when (3) put  x1-x3;
when (4) put  x1-x4;
when (5) put  x1-x5;
when (6) put  x1-x6;
when (7) put  x1-x7;
when (8) put  x1-x8;
end;
end;
end;
run;
```

For added interest use the ALLPERM to generate all the permutations of order the reindeer could be harnessed to the sleigh.

SAS Super FREQ

## Re: Juletip #15 - Listing all possible combinations of Santa's deers

For more about how all the combinations that Santa can use to pull his sleigh, see

"Working with combinations in SAS"

Discussion stats
• 3 replies
• 542 views
• 10 likes
• 4 in conversation