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 Section

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;

4 by 5 Factorial Design

Obs

i

j

1

1

1

2

1

2

3

1

3

4

1

4

5

1

5

6

2

1

7

2

2

8

2

3

9

2

4

10

2

5

11

3

1

12

3

2

13

3

3

14

3

4

15

3

5

16

4

1

17

4

2

18

4

3

19

4

4

20

4

5

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, 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 Section

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;

4 by 5 Factorial Design

Obs

i

j

1

10

3

2

10

6

3

10

9

4

10

12

5

10

15

6

20

3

7

20

6

8

20

9

9

20

12

10

20

15

11

30

3

12

30

6

13

30

9

14

30

12

15

30

15

16

40

3

17

40

6

18

40

9

19

40

12

20

40

15

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