We now turn our focus from randomly sampling a subset of observations from a data set to that of generating a random assignment of treatments to experimental units in a randomized, controlled experiment. The good news is that the techniques used to sample without replacement can easily be extended to generate such random assignment plans.
It's probably a good time to remind you of the existence of the PLAN procedure. As I mentioned earlier, due to time constraints of the course and the complexity of the PLAN procedure, we will not use it to accomplish any of our random assignments. You should be aware, however, of its existence should you want to explore it on your own in the future.
Example 34.15 Section
Suppose we are interested in conducting an experiment so that we can compare the effects of two drugs (A and B) and one placebo on headache pain. We have 30 subjects enrolled in our study, but need to determine a plan for randomly assigning 10 of the subjects to treatment A, 10 of the subjects to treatment B, and 10 of the subjects to the placebo. The following program does just that for us. That is, it creates a random assignment for 30 subjects in a completely randomized design with one factor having 3 levels:
DATA exper1; DO Unit = 1 to 30; OUTPUT; END; RUN; DATA random1; set exper1; random=ranuni(27407349); RUN; PROC SORT data=random1; by random; RUN; PROC FORMAT; value trtfmt 1 = 'Placebo' 2 = 'Drug A' 3 = 'Drug B'; RUN; DATA random1; set random1; if _N_ le 10 then group=1; else if _N_ gt 10 and _N_ le 20 then group=2; else if _N_ gt 20 then group=3; format group trtfmt.; RUN; PROC PRINT data = random1 NOOBS; title 'Random Assignment for CRD with One Factor'; RUN
Okay, let's have you first launch and run the SAS program, so you can review the resulting output to convince yourself that the code did indeed generate the desired treatment plan. You should see that 10 of the subjects were randomly assigned to treatment A, 10 to treatment B, and 10 to the placebo.
Now, let's walk ourselves through the program to make sure we understand how it works. The first DATA step merely uses a simple DO loop to create a temporary data set called exper1 that contains one observation for each of the experimental units (in our case, the experimental units are subjects). The only variable in the data set, unit, contains an arbitrary label 1, 2, ..., 30 assigned to each of the experimental units.
The remainder of the code generates the random assignment. To do so, the code from Example 34.5 is simply extended. That is:
- The second DATA step uses the ranuni function to generate a uniform random number between 0 and 1 for each observation in the exper1 data set. The result is stored in a temporary data set called random1.
- The random1 data set is sorted in order of the random number.
- The third DATA step uses an IF-THEN-ELSE construct to assign the first ten units in sorted order to group 1, the second ten to group 2, and the last ten to group 3.
- A FORMAT is defined to label the groups meaningfully.
- The final randomization list is printed.
Example 34.16 Section
To create a random assignment for a completely randomized design with two factors, you can just modify the IF statement in the previous example. The following program generates a random assignment of treatments to 30 subjects, in which Factor A has 2 levels and Factor B has 3 levels (and hence 6 treatments). The code is similar to the code from the previous example except the IF statement now divides the 30 subjects into 6 treatment groups and (arbitrarily) assigns the levels of factors A and B to the groups:
DATA random2; set exper1; random=ranuni(4901); RUN; PROC SORT data=random2; by random; RUN; DATA random2; set random2; if _N_ le 5 then do; factorA = 1; factorB = 1; end; else if _N_ gt 5 and _N_ le 10 then do; factorA = 1; factorB = 2; end; else if _N_ gt 10 and _N_ le 15 then do; factorA = 1; factorB = 3; end; else if _N_ gt 15 and _N_ le 20 then do; factorA = 2; factorB = 1; end; else if _N_ gt 20 and _N_ le 25 then do; factorA = 2; factorB = 2; end; else if _N_ gt 25 and _N_ le 30 then do; factorA = 2; factorB = 3; end; RUN; PROC PRINT data = random2; title 'Random Assignment for CRD with Two Factors'; RUN;
First, my apologies about the formatting that makes the IF-THEN-ELSE statement a little difficult to read. I needed to format it as such so that I could easily capture the image of the program for you.
Again, it's probably best if you first launch and run the SAS program, so you can review the resulting output to convince yourself that the code did indeed generate the desired treatment plan. You should see that five of the subjects were randomly assigned to the A=1, B=1 group, five to the A=1, B=2 group, five to the A=1, B=3 group, and so on.
Then, if you compare the code to the code from the previous example, the only substantial difference you should see is the difference betwen the two IF statements. As previously mentioned, the IF statement here divides the 30 subjects into 6 treatment groups and (arbitrarily) assigns the levels of factors A and B to the groups:
Example 34.17 Section
Thus far, our random assignments have not involved dealing with a blocking factor. As you know, it is natural in some experiments to block some of the experimental units together in an attempt to reduce unnecessary variability in your measurements that might otherwise prevent you from making good treatment comparisons. Suppose, for example, that your workload would prevent you from making more than nine experimental measurements in a day. Then, it would be a good idea then to treat day as a blocking factor. The following program creates a random assignment for 27 subjects in a randomized block design with one factor having three levels.
DATA exper2 (drop = j); DO block = 1 to 3; DO j = 1 to 9; if block = 1 then do; unit = j; output; end; else if block = 2 then do; unit = j + 9; output; end; else if block = 3 then do; unit = j + 18; output; end; END; END; RUN; PROC PRINT data=exper2; title 'EXPER2: Definition of Experimental Units'; RUN; DATA random3; set exper2; random=ranuni(7214508); RUN; PROC SORT data=random3; by block random; RUN; DATA random3; set random3; by block; if first.block then k=0; else k=k+1; if k in (0,1,2) then trt=1; else if k in (3,4,5) then trt=2; else if k in (6,7,8) then trt=3; retain k; RUN; PROC PRINT data=random3 noobs; title 'Random Assignment for RBD: Sorted in BLOCK-TRT Order'; RUN; PROC SORT data=random3; by block unit; RUN; PROC PRINT data=random3 noobs; title 'Random Assignment for RBD: Sorted in BLOCK-UNIT Order'; RUN;
Again, my apologies about the formatting that makes the program a little more difficult than usual to read. I needed to format it as such so that I could easily capture the image of the program for you.
It's probably going to be best if you first launch and run the SAS program, so you can first review the contents of the initial exper2 data set:
EXPER2: Definition of Experimental Units
and then the resulting output that contains the desired treatment plan... first in block-treatment order:
Random Assignments for RBD: Sorted in BLOCK-TRT order
and then in block-unit order:
Random Assignments for RBD: Sorted in BLOCK-UNIT order
As you can see, the exper2 data set is created to contain one observation for each of the experimental units (27 subjects here). The variable unit contains an arbitrary label (1, 2, ..., 30) assigned to each of the experimental units. The variable block, which identifies the block number (1, 2, and 3), divides the experimental units up into three equal-sized blocks of nine.
Now, to create the random assignment:
- We use the ranuni function generate a uniform random number between 0 and 1 for each observation.
- Then, within each block, we sort the data in order of the random number.
- Then, we create a counter variable to count the number of observations within each block: for the first observation within each block ("if first.block"), we set the counter (k) to 0; otherwise we increase the counter by 1 for each observation within the block. (For this to work, we must retain k from iteration to iteration).
- Using an IF-THEN-ELSE construct, within each block, assign the first three units in sorted order (k=0,1,2) to group 1, the second three (k=3,4,5) to group 2, and the last three (k=6,7,8) to group 3.
Depending on how the experiment will be conducted, you can print the random assignment in different orders:
- First, the randomization is printed in order of treatment within each block. This will accommodate experiments for which it is natural to perform the treatments in groups on the randomized experimental units.
- Then, the randomization is printed in order of units within block. This will accommodate experiments for which it is natural to perform the treatments in random order on consecutive experimental units.