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 nodate pageno=1 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
;
proc print data=changed;
title 'Number of Books Sold';
run;
The following output
shows the CHANGED data set.
Using an Array Statement to Process Missing Data Values
Number of Books Sold 1
Obs Reference Usage Introduction
1 45 63 113
2 0 75 150
3 62 0 98