Table 23.2 DO Statement Processing
DO Statement Description
do i=2 to 4;
processes elements 2 through 4
do i=1 to 7 by 2;
processes elements 1, 3, 5, and 7
do i=3,5;
processes elements 3 and 5
Selecting the Current Variable
You must tell SAS which variable in the array to use in each iteration of the loop. Recall
that you identify variables in an array by their array references and that you use a
variable name, a number, or an expression as the subscript of the reference. Therefore,
you can write programming statements so that the index variable of the DO loop is the
subscript of the array reference (for example, array-name{index-variable}). When the
value of the index variable changes, the subscript of the array reference (and therefore
the variable that is referenced) also changes.
The following example uses the index variable count as the subscript of array references
inside a DO loop:
array books{3} Reference Usage Introduction;
do count=1 to 3;
if books{count}=. then books{count}=0;
end;
When the value of count is 1, SAS reads the array reference as Books{1} and processes
the IF-THEN statement on Books{1}, which is the variable Reference. When count is 2,
SAS processes the statement on Books{2}, which is the variable Usage. When count is
3, SAS processes the statement on Books{3}, which is the variable Introduction.
The statements in the example tell SAS to
• perform the actions in the loop three times
• replace the array subscript count with the current value of count for each iteration of
the IF-THEN statement
• locate the variable with that array reference and process the IF-THEN statement on it
• replace missing values with zero if the condition is true.
The following DATA step defines the array Book and processes it with a DO loop.
options linesize=80 pagesize=60;
data changed(drop=count);
input Reference Usage Introduction;
array book{3} Reference Usage Introduction;
do count=1 to 3;
if book{count}=. then book{count}=0;
end;
datalines;
45 63 113
. 75 150
62 . 98
560 Chapter 23 • Array Processing