The PLAN Procedure

Example 86.6 Permutations and Combinations

Occasionally, you might need to generate all possible permutations of n things, or all possible combinations of n things taken m at a time.

For example, suppose you are planning an experiment in cognitive psychology where you want to present four successive stimuli to each subject. You want to observe each permutation of the four stimuli. The following statements use PROC PLAN to create a data set containing all possible permutations of four numbers in random order.

title 'All Permutations of 1,2,3,4';
proc plan seed=60359;
   factors    Subject  = 24
              Order    = 4  ordered;
   treatments Stimulus = 4  perm;
   output out=Psych;
run;
proc sort data=Psych out=Psych;
   by Subject Order;
run;
proc transpose data= Psych(rename=(Order=_NAME_))
               out =tPsych(drop=_NAME_);
   by Subject;
   var Stimulus;
run;
proc print data=tPsych noobs;
run;

The variable Subject is set at 24 levels because there are $4!=24$ total permutations to be listed. If $\Variable{Subject}>24$, the list repeats. Output 86.6.1 displays the PROC PLAN output. Note that the variable Subject is listed in random order.

Output 86.6.1: List of Permutations

All Permutations of 1,2,3,4

The PLAN Procedure

Plot Factors
Factor Select Levels Order
Subject 24 24 Random
Order 4 4 Ordered

Treatment Factors
Factor Select Levels Order
Stimulus 4 4 Perm

Subject Order Stimulus
4 1 2 3 4 1 2 3 4
15 1 2 3 4 1 2 4 3
24 1 2 3 4 1 3 2 4
1 1 2 3 4 1 3 4 2
5 1 2 3 4 1 4 2 3
17 1 2 3 4 1 4 3 2
19 1 2 3 4 2 1 3 4
14 1 2 3 4 2 1 4 3
6 1 2 3 4 2 3 1 4
23 1 2 3 4 2 3 4 1
8 1 2 3 4 2 4 1 3
2 1 2 3 4 2 4 3 1
13 1 2 3 4 3 1 2 4
16 1 2 3 4 3 1 4 2
12 1 2 3 4 3 2 1 4
18 1 2 3 4 3 2 4 1
21 1 2 3 4 3 4 1 2
9 1 2 3 4 3 4 2 1
22 1 2 3 4 4 1 2 3
10 1 2 3 4 4 1 3 2
7 1 2 3 4 4 2 1 3
11 1 2 3 4 4 2 3 1
3 1 2 3 4 4 3 1 2
20 1 2 3 4 4 3 2 1



The output data set Psych contains 96 observations of the 3 variables (Subject, Order, and Stimulus). Sorting the output data set by Subject and by Order within Subject results in all possible permutations of Stimulus in random order. PROC TABULATE displays these permutations in Output 86.6.2.

Output 86.6.2: Randomized Permutations

All Permutations of 1,2,3,4

Subject _1 _2 _3 _4
1 1 3 4 2
2 2 4 3 1
3 4 3 1 2
4 1 2 3 4
5 1 4 2 3
6 2 3 1 4
7 4 2 1 3
8 2 4 1 3
9 3 4 2 1
10 4 1 3 2
11 4 2 3 1
12 3 2 1 4
13 3 1 2 4
14 2 1 4 3
15 1 2 4 3
16 3 1 4 2
17 1 4 3 2
18 3 2 4 1
19 2 1 3 4
20 4 3 2 1
21 3 4 1 2
22 4 1 2 3
23 2 3 4 1
24 1 3 2 4



As another example, suppose you have six alternative treatments, any four of which can occur together in a block (in no particular order). The following statements use PROC PLAN to create a data set containing all possible combinations of six numbers taken four at a time. In this case, you use ODS to create the data set.

title 'All Combinations of (6 Choose 4) Integers';
proc plan;
   factors Block=15 ordered
           Treat= 4 of 6 comb;
   ods output Plan=Combinations;
run;
proc print data=Combinations noobs;
run;

The variable Block has 15 levels since there are a total of $6!/(4!2!)=15$ combinations of four integers chosen from six integers. The data set formed by ODS from the displayed plan has one row for each block, with the four values of Treat corresponding to four different variables, as shown in Output 86.6.3 and Output 86.6.4.

Output 86.6.3: List of Combinations

All Combinations of (6 Choose 4) Integers

The PLAN Procedure

Factor Select Levels Order
Block 15 15 Ordered
Treat 4 6 Comb

Block Treat
1 1 2 3 4
2 1 2 3 5
3 1 2 3 6
4 1 2 4 5
5 1 2 4 6
6 1 2 5 6
7 1 3 4 5
8 1 3 4 6
9 1 3 5 6
10 1 4 5 6
11 2 3 4 5
12 2 3 4 6
13 2 3 5 6
14 2 4 5 6
15 3 4 5 6



Output 86.6.4: Combinations Data Set Created by ODS

All Combinations of (6 Choose 4) Integers

Block Treat1 Treat2 Treat3 Treat4
1 1 2 3 4
2 1 2 3 5
3 1 2 3 6
4 1 2 4 5
5 1 2 4 6
6 1 2 5 6
7 1 3 4 5
8 1 3 4 6
9 1 3 5 6
10 1 4 5 6
11 2 3 4 5
12 2 3 4 6
13 2 3 5 6
14 2 4 5 6
15 3 4 5 6