18.2 - Nesting Do Loops

18.2 - Nesting Do Loops

One way to make iterative DO loops even more powerful is to place one DO loop inside of another. Putting a DO loop within another DO loop is called nesting. We'll take a look at a few examples here.

Example 18.4

Suppose you are interested in conducting an experiment with two factors A and B. Suppose factor A is, say, the amount of water with levels 1, 2, 3, and 4; and factor B is, say, the amount of sunlight, say with levels 1, 2, 3, 4, and 5. Then, the following SAS code uses nested iterative DO loops to generate the 4 by 5 factorial design:

    DATA design;
DO i = 1 to 4;
DO j = 1 to 5;
output;
END;
END;
RUN;

PROC PRINT data = design;
TITLE '4 by 5 Factorial Design';
RUN;

First, launch and run the SAS program. Then, review the output from the PRINT procedure to see the contents of the design data set. By doing so, you can get a good feel for how the nested DO loops work. First, SAS sets the value of the index variable i to 1, then proceeds to the next step which happens to be another iterative DO loop. While i is 1:

• SAS sets the value of j to 1, and outputs the observation in which i = 1 and j = 1.
• Then, SAS sets the value j to 2, and outputs the observation in which i = 1 and j = 2.
• Then, SAS sets the value j to 3, and outputs the observation in which i = 1 and j = 3.
• Then, SAS sets the value j to 4, and outputs the observation in which i = 1 and j = 4.
• Then, SAS sets the value j to 5, and outputs the observation in which i = 1 and j = 5.
• Then, SAS sets the value j to 6, and jumps out of the inside DO loop and proceeds to the next statement, which happens to be the end of the outside DO loop.

SAS then sets the value of the index variable i to 2, then proceeds through the inside DO loop again just as described above. This process continues until SAS sets the value of index variable i to 5, jumps out of the outside DO loop, and ends the DATA step.

Example 18.5

Back to our experiment with two factors A and B. Suppose this time that factor A is, say, the amount of water with levels 10, 20, 30, and 40 liters; and factor B is, say, the amount of sunlight, say with levels 3, 6, 9, 12, and 15 hours. The following SAS code uses two DO loops with BY options to generate a more meaningful 4 by 5 factorial design that corresponds to the exact levels of the factors:

    DATA design;
DO i = 10 to 40 by 10;
DO j = 3 to 15 BY 3;
output;
END;
END;
RUN;

PROC PRINT data = design;
TITLE '4 by 5 Factorial Design';
RUN;

First, launch and run the SAS program. Then, review the output from the PRINT procedure to see the contents of the design data set. By doing so, you can get a good feel for how the nested DO loops with BY options work. First, SAS sets the value of the index variable i to 10, then proceeds to the next step which happens to be another iterative DO loop. While i is 10:

• SAS sets the value of j to 3, and outputs the observation in which i = 10 and j = 3.
• Then, SAS sets the value j to 6, and outputs the observation in which i = 10 and j = 6.
• Then, SAS sets the value j to 9, and outputs the observation in which i = 10 and j = 9.
• Then, SAS sets the value j to 12, and outputs the observation in which i = 10 and j = 12.
• Then, SAS sets the value j to 15, and outputs the observation in which i = 10 and j = 15.
• Then, SAS sets the value j to 18, and jumps out of the inside DO loop and proceeds to the next statement, which happens to be the end of the outside DO loop.

SAS then sets the value of the index variable i to 20, then proceeds through the inside DO loop again just as described above. This process continues until SAS sets the value of index variable i to 50, jumps out of the outside DO loop, and ends the DATA step.

 [1] Link ↥ Has Tooltip/Popover Toggleable Visibility