Processing Variables with Arrays |
Creating Multidimensional
Arrays![]() ![]() |
Referencing Elements of a Two-Dimensional Array
Multidimensional arrays are typically used with nested DO loops. The next example uses a one-dimensional array, a two-dimensional array, and a nested DO loop to restructure a set of variables. Your company's sales figures are stored by month in the SAS data set Finance.Monthly. Your task is to generate a new data set of quarterly sales rather than monthly sales. |
Description of Finance.Monthly
Variable
Type
Length
Year
num
8
Month1
num
8
Month2
num
8
Month3
num
8
Month4
num
8
Month5
num
8
Month6
num
8
Month7
num
8
Month8
num
8
Month9
num
8
Month10
num
8
Month11
num
8
Month12
num
8
Defining the array m(4,3) puts the variables
Month1 through Month12 into four groups of three
months (yearly quarters). |
Table Representation of m Array | |||||||||||
Month1 | Month2 | Month3 |
Month4 | Month5 | Month6 |
Month7 | Month8 | Month9 |
Month10 | Month11 | Month12 |
data finance.quarters; set finance.monthly; array m(4,3) month1-month12; |
Defining the array Qtr(4) creates the numeric variables
Qtr1 , Qtr2 , Qtr3 , Qtr4 ,
which will be used to sum the sales for each quarter.
data finance.quarters; set finance.monthly; array m(4,3) month1-month12; array Qtr(4);
A nested DO loop is used to reference the values of the variables
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; The first DO loop executes once for each element of the The assignment statement, 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;
The second DO loop executes the same number of times as the second dimension
of the 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; To see how the nested DO loop processes these arrays, let's step through the execution of this DATA step. |
![]() ![]() ![]() ![]() ![]() ![]() |
|
![]() |
![]() |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.