SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

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 Month1 through Month12 and calculate the values of Qtr1 through Qtr4. Because the variables i and j are used only for loop processing, the DROP= option is used to exclude them from the Finance.Quarters data set.

     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 Qtr array. This is also the same value as the first dimension of the M array.

The assignment statement, qtr(i)=0, sets the value of qtr(i)to zero after each iteration of the first DO loop. Without the assignment statement, the values of Qtr1, Qtr2, Qtr3, and Qtr4 would accumulate across iterations of the data step due to the qtr(i)+m(i,j) sum statement within the DO loop.

     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 M array.

     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.


  back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement