Chapter Contents

Previous

Next

Multidimensional Arrays


Grouping Variables in a Multidimensional Array

To create a multidimensional array, place the number of elements in each dimension after the array name in the form {n, ... } where n is required for each dimension of a multidimensional array.

From right to left, the rightmost dimension represents columns; the next dimension represents rows. Each position farther left represents a higher dimension. The following ARRAY statement defines a two-dimensional array with two rows and five columns. The array contains ten variables: five temperature measures (t1 through t5) from two cities (c1 and c2):

array temprg{2,5} c1t1-c1t5 c2t1-c2t5;

SAS places variables into a multidimensional array by filling all rows in order, beginning at the upper-left corner of the array (known as row-major order). You can think of the variables as having the following arrangement:

c1t1 c1t2 c1t3 c1t4 c1t5
c2t1 c2t2 c2t3 c2t4 c2t5

To refer to the elements of the array later with an array reference, you can use the array name and subscripts. The following table lists some of the array references for the previous example:

Variable Array reference
c1t1 temprg{1,1}
c1t2 temprg{1,2}
c2t2 temprg{2,2}
c2t5 temprg{2,5}


Using Nested DO Loops

Multidimensional arrays are usually processed inside nested DO loops. As an example, the following is one form that processes a two-dimensional array:

DO index-variable-1=1 TO number-of-rows;
DO index-variable-2=1 TO number-of-columns;
... more SAS statements ...
END;
END;

An array reference can use two or more index variables as the subscript to refer to two or more dimensions of an array. Use the following form:

array-name {index-variable-1, ...,index-variable-n}

The following example creates an array that contains ten variables- five temperature measures (t1 through t5) from two cities (c1 and c2). The DATA step contains two DO loops.

For each iteration of the DO loops, SAS substitutes the value of the array element corresponding to the current values of I and J.

options nodate pageno=1 linesize=80 pagesize=60;

data temps;
   array temprg{2,5} c1t1-c1t5 c2t1-c2t5;
   input c1t1-c1t5 /
         c2t1-c2t5;
   do i=1 to 2;
     do j=1 to 5;
       temprg{i,j}=round(temprg{i,j});
     end;
   end;
   datalines;
89.5 65.4 75.3 77.7 89.3
73.7 87.3 89.9 98.2 35.6
75.8 82.1 98.2 93.5 67.7
101.3 86.5 59.2 35.6 75.7
;

proc print data=temps;
   title 'Temperature Measures for Two Cities';
run;

The following data set TEMPS contains the values of the variables rounded to the nearest whole number.

Using a Multidimensional Array
                      Temperature Measures for Two Cities                      1

     Obs  c1t1  c1t2  c1t3  c1t4  c1t5  c2t1  c2t2  c2t3  c2t4  c2t5  i  j

      1    90    65    75    78    89     74   87    90    98    36   3  6
      2    76    82    98    94    68    101   87    59    36    76   3  6

The previous example can also use the DIM function to produce the same result:

do i=1 to dim1(temprg);
   do j=1 to dim2(temprg);
      temprg{i,j}=round(temprg{i,j});
   end;
end;
The value of DIM1(TEMPRG) is 2; the value of DIM2(TEMPRG) is 5.


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.