Processing Variables with Arrays |
Creating Multidimensional
Arrays![]() ![]() |
When this DATA step is compiled, the program data vector is created
containing the variables Year , Month1 through
Month12 , and the new variables Qtr1 through
Qtr4 . (Only the beginning and ending portions of the program
data vector are represented here.)
data finance.quarters(drop=i j); set finance.monthly; array m(4,3) month1-month12; array Qtr(4); do i=1 to 4; qtr(i=0); do j=1 to 3; qtr(i)+m(i,j); end; end; run; |
During the initial execution of the DATA step, the values of the first
observation of Finance.Monthly are read into the program
data vector. When the first DO loop executes the first time, the index
variable i is set to 1.
data finance.quarters(drop=i j); set finance.monthly; array m(4,3) month1-month12; array Qtr(4); > do i=1 to 4; i=1 qtr(i=0); do j=1 to 3; qtr(i)+m(i,j); end; end; run; |
During the first iteration of the nested DO loop, the value of Month1 ,
referenced by m(i,j) , is added to Qtr1 .
data finance.quarters(drop=i j); set finance.monthly; array m(4,3) month1-month12; array Qtr(4); do i=1 to 4; i=1 qtr(i=0); > do j=1 to 3; j=1 qtr(1)+m(1,1); end; end; run; |
During the second iteration of the nested DO loop, the value of Month2 ,
referenced by m (i,j ), is added to
Qtr1 .
data finance.quarters(drop=i j); set finance.monthly; array m(4,3) month1-month12; array Qtr(4); do i=1 to 4; i=1 qtr(i=0); > do j=1 to 3; j=2 qtr(1)+m(1,2); end; end; run; |
The nested DO loop continues to execute until the index variable j
exceeds the stop value, 3. When the nested DO loop completes
execution, the total sales for the first quarter, Qtr1 , has
been computed.
data finance.quarters(drop=i j); set finance.monthly; array m(4,3) month1-month12; array Qtr(4); do i=1 to 4; i=1 qtr(i=0); > do j=1 to 3; j=3 qtr(1)+m(1,3); end; end; run; |
The outer DO loop increments to 2 and the process continues
for the array element Qtr2 and the m array elements
Month4 through Month6 .
data finance.quarters(drop=i j); set finance.monthly; array m(4,3) month1-month12; array Qtr(4); > do i=1 to 4; i=2 qtr(i=0); do j=1 to 3; j=1 qtr(i)+m(i,j); end; end; run; |
After the outer DO loop completes execution, the end of the DATA step
is reached and the variable values for the first observation are written
to the data set Finance.Quarters.
data finance.quarters(drop=i j); set finance.monthly; array m(4,3) month1-month12; array Qtr(4); > do i=1 to 4; i=5 (loop ends) qtr(i=0); do j=1 to 3; qtr(i)+m(i,j); end; end; run; |
What you have seen so far represents the first iteration of the DATA step. All observations in the data set Finance.Monthly are processed in the same manner. Below is a portion of the resulting data set, which contains the sales figures grouped by quarters. |
SAS Data Set Finance.Quarters (Partial Listing)
Year
Qtr1
Qtr2
Qtr3
Qtr4
1989
69100
64400
69200
71800
1990
73100
72000
83200
82800
1991
73400
81800
85200
87800
![]() ![]() ![]() ![]() ![]() ![]() |
|
![]() |
![]() |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.