# 34.4 - Creating Random Assignments

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.** 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:

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.

!!!! **NOTE** !!!!! The randomization list created here contains information that is potentially damaging to the success of the whole study if it ended up in the wrong hands. That is, blinding would be violated. It is better (and more common) practice to keep separate master lists which associate *unit* with the subject's name, and *group* number with treatment name. In many national trials, it is common to have statisticians also blinded from the master list, producing a "triple-blind" trial. I formatted treatment here just for illustration purposes* * only.

**Example 34.16.** 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:

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

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:

and then the resulting output that contains the the desired treatment plan... first in block-treatment order:

and then 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.