Processing Variables with Arrays |
Creating One-Dimensional
Arrays |
Next, let's look at an example of a DATA step that contains an array
with a DO loop.
The Health Center of a company conducts a fitness class for its employees. Each week, participants are weighed so they can monitor their progress. The weight data, currently stored in kilograms, needs to be converted to pounds. |
SAS Data Set Hrd.Fitclass
Name
Weight1
Weight2
Weight3
Weight4
Weight5
Weight6
Alicia
69.6
68.9
68.8
67.4
66.0
66.2
Betsy
52.6
52.6
51.7
50.4
49.8
49.1
Brenda
68.6
67.6
67.0
66.4
65.8
65.2
Carl
67.6
66.6
66.0
65.4
64.8
64.2
Carmela
63.6
62.5
61.9
61.4
60.8
58.2
David
70.6
69.8
69.2
68.4
67.8
67.0
You can use a DO loop to update the variables Weight1 through
Weight6 for each observation in the
Hrd.Fitclass data set.
data hrd.convert; set hrd.fitclass; array wt(6) weight1-weight6; do i=1 to 6; wt(i)=wt(i)*2.2046; end; run; |
The assignment statement wt(i) on
the left side of the equal (= ) sign is an array reference, not
a variable name, so it does not violate the rule against having the same
variable and array name in a DATA step. |
Compilation and Execution
To understand how the DO loop processes the array elements, let's step through the compilation and execution phases of this DATA step. During compilation, the program data vector is created for the Hrd.Convert data set. |
The DATA step is scanned for syntax errors. If there are any syntax errors
in the ARRAY statement, they are detected at this time.
The index values of the array elements are assigned. Note that the array name and array references are not included in the program data vector. The array name and array references exist only for the duration of the DATA step. During the first iteration of the DATA step, the first observation in Hrd.Fitclass is read into the program data vector. data hrd.convert; set hrd.fitclass; array wt(6) weight1-weight6; do i=1 to 6; wt(i)=wt(i)*2.2046; end; run; |
Because the ARRAY statement is a compile-time only statement, it is ignored
during execution. The DO loop is executed next.
During the first iteration of the DO loop, the index variable data hrd.convert; set hrd.fitclass; array wt(6) weight1-weight6; do i=1 to 6; wt(1)=wt(1)*2.2046; end; run; |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.