7

Juletip #15 - Listing all possible combinations of Santa's deers - SAS Support C...

 1 year ago
source link: https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-15-Listing-all-possible-combinations-of-Santa-s-deers/m-p/849796
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Listing all possible combinations of Santa's deers

Posted Thursday (228 views)

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

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;


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK