An end-of-file condition occurs when you try to read past the end of a data set or when you point to an observation that exceeds the number of observations in a data set. If an end-of-file condition occurs inside a DO DATA iteration loop, control passes to the first statement after the DO DATA loop.
The following example uses a DO DATA loop to read observations from the Sashelp.Class
data set. The loop reads the data one observation at a time and accumulates the weights of the students in the SAS/IML matrix
named sum
. After the data are read, the variable sum
contains the sum of the weights for the class.
proc iml; use Sashelp.Class; /* if data set already open, use setin class point 0; */ sum=0; do data; read next var{weight}; sum = sum + weight; end; print sum;
Figure 7.20: Sum of Data Values
sum |
---|
1900.5 |
The example shows how to read data one observation at a time within a DO loop. However, there are more efficient ways to read
data in the SAS/IML language. If all the data can fit into memory, it is more efficient to read all the data into a vector
and use vector operations to compute statistical quantities such as a sum. For example, the following statements compute the
same quantity (the sum of the Weight
variable) but are more efficient:
read all var{weight}; sum = sum(weight);