The PLAN Procedure

Example 87.6 Permutations and Combinations

(View the complete code for this example.)

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 total permutations to be listed. If , the list repeats. Output 87.6.1 displays the PROC PLAN output. Note that the variable Subject is listed in random order.

Output 87.6.1: List of Permutations

All Permutations of 1,2,3,4

The PLAN Procedure

Plot Factors
FactorSelectLevelsOrder
Subject2424Random
Order44Ordered

Treatment Factors
FactorSelectLevelsOrder
Stimulus44Perm

SubjectOrderStimulus
412341234
1512341243
2412341324
112341342
512341423
1712341432
1912342134
1412342143
612342314
2312342341
812342413
212342431
1312343124
1612343142
1212343214
1812343241
2112343412
912343421
2212344123
1012344132
712344213
1112344231
312344312
2012344321


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 87.6.2.

Output 87.6.2: Randomized Permutations

All Permutations of 1,2,3,4

Subject_1_2_3_4
11342
22431
34312
41234
51423
62314
74213
82413
93421
104132
114231
123214
133124
142143
151243
163142
171432
183241
192134
204321
213412
224123
232341
241324


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 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 87.6.3 and Output 87.6.4.

Output 87.6.3: List of Combinations

All Combinations of (6 Choose 4) Integers

The PLAN Procedure

FactorSelectLevelsOrder
Block1515Ordered
Treat46Comb

BlockTreat
11234
21235
31236
41245
51246
61256
71345
81346
91356
101456
112345
122346
132356
142456
153456


Output 87.6.4: Combinations Data Set Created by ODS

All Combinations of (6 Choose 4) Integers

BlockTreat1Treat2Treat3Treat4
11234
21235
31236
41245
51246
61256
71345
81346
91356
101456
112345
122346
132356
142456
153456