7

Generate combinations in SAS

 1 year ago
source link: https://blogs.sas.com/content/iml/2013/09/30/generate-combinations-in-sas.html
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.
neoserver,ios ssh client

Generate combinations in SAS

16

Last week I described how to generate permutations in SAS. A related concept is the "combination." In probability and statistics, a combination is a subset of k items chosen from a set that contains N items. Order does not matter, so although the ordered triplets (B, A, C) and (C, A, B) represent different permutations of three items, they are considered equivalent when regarded as sets. Consequently, a combination is usually presented in a canonical format, such as in alphanumeric order: {A, B, C}.

SAS/IML 9.3 introduced two functions for working with combinations: the ALLCOMB function, which enumerates all combinations, and the RANCOMB function, which generates random combinations.

Let's see how these functions could be used. I often eat salads for lunch. Suppose that each day I add three toppings to my lettuce, chosen from my five favorite items on the salad bar. If I want to create a schedule that rotates through all combinations of possible salad toppings, I could run the following SAS/IML program. The program uses the ALLCOMB function to enumerate the set of all combinations of five items chosen three at a time:

proc iml;
N = 5;                        /* number of possible toppings */          
k = 3;                        /* number of toppings per salad */
idx = allcomb(N, k);                    
print idx;

t_combin1.png

Each of the ten rows in the matrix is a combination of veggies. The first row corresponds to a salad with veggies 1, 2, and 3. To make it easier to read, I can name the five toppings and use these indices to extract the names:

Items = {"Broccoli" "Carrots" "Cucumbers" "Peppers" "Tomatoes"};
S = Items[ ,idx];
S = shape(S, 0, k);           /* reshape S to have 3 cols */
print S[r=(char(1:nrow(S))) L="Salad Schedule"];
t_combin2.png

It will take me ten lunches to cycle through the distinct combinations of salad toppings. (Use the COMB function to compute "N choose k," which is the total number of combinations.) However, if I am feeling particularly daring—some would say a tad wild!—I could forego my orderly progression through the schedule and generate a random combination of veggies for my salad by using the RANCOMB function, as follows:

Toppings = rancomb(Items, 3);
print Toppings[L="Today's Toppings"];

t_combin3.png

Although the salad example is intentionally whimsical, combinations are serious business in statistics, especially in the combinatorial design of experiments. In addition to the SAS/IML functions, the PLAN procedure in SAS/STAT software enables you to enumerate all combinations of elements, or to use random combinations in designing an experiment.

Furthermore, the SAS DATA step has multiple functions for working with combinations. The ALLCOMB function and several similar functions enable you to generate all combinations of elements that are contained in a DATA step array. The RANCOMB subroutine enables you to randomly permute elements in an array.

In summary, SAS software provides a "combination" of choices for choosing k items from a set of N possibilities.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK