Two-dimensional arrays are straightforward extensions of one-dimensional arrays. You can think of one-dimensional arrays such as the array barkers:
ARRAY barkers(4) dog1-dog4;
as a single row of variables:
dog1 dog2 dog3 dog4
And two-dimensional arrays such as the array pets:
ARRAY pets(2,4) dog1-dog4 cat1-cat4;
as multiple rows of variables:
dog1 dog2 dog3 dog4
cat1 cat2 cat3 cat4
As the previous ARRAY statement suggests, to define a two-dimensional array, you specify the number of elements in each dimension, separated by a comma. In general, the first dimension number tells SAS how many rows your array needs, while the second dimension number tells SAS how many columns your array needs.
When you define a two-dimensional array, the array elements are grouped in the order in which they appear in the ARRAY statement. For example, SAS assigns the elements of the array horse:
ARRAY horse(3,5) x1-x15;
as follows:
Array Example | |||||
---|---|---|---|---|---|
x1 | x2 | x3 | x4 | x5 | |
x6 | x7 | x8 | x9 | x10 | |
x11 | x12 | x13 | x14 | x15 |
In this section, we'll look at two examples that involve checking a subset of Family History data for missing values. In the first example, we'll use two one-dimensional arrays — the first array to store the actual data and the second array to store binary status variables that indicate whether a particular data value is missing or not. In the second example, we'll use one two-dimensional array — the first dimension to store the actual data and the second dimension to store binary status variables that indicate whether a particular data value is missing or not.
Example 19.18 Section
The following program reads 14 family history variables (fhx1, ..., fhx14) arising from five subjects and stores the data in a one-dimensional array called edit. Then, SAS searches the edit array to determine whether or not any data values are missing. Fourteen (14) status variables (stat1, ..., stat14) are created to correspond to each of the 14 data variables. The status (1 if missing and 0 if nonmissing) is stored in another one-dimensional array called status:
DATA fhx;
input subj v_date mmddyy8. fhx1-fhx14;
array edit(14) fhx1-fhx14;
array status(14) stat1-stat14;
do i = 1 to 14;
status(i) = 0;
if edit(i) = . then status(i) = 1;
end;
DATALINES;
220004 07/27/93 0 0 0 . 8 0 0 1 1 1 . 1 0 1
410020 11/11/93 0 0 0 . 0 0 0 0 0 0 . 0 0 0
520013 10/29/93 0 0 0 . 0 0 0 0 0 0 . 0 0 1
520068 08/10/95 0 0 0 0 0 1 1 0 0 1 1 0 1 0
520076 08/25/95 0 0 0 0 1 8 0 0 0 1 1 0 0 1
;
RUN;
PROC PRINT data = fhx;
var fhx1-fhx14;
TITLE 'The FHX data itself';
RUN;
PROC PRINT data = fhx;
var stat1-stat14;
TITLE 'The presence of missing values in FHX data';
RUN;
Obs | fhx1 | fhx2 | fhx3 | fhx4 | fhx5 | fhx6 | fhx7 | fhx8 | fhx9 | fhx10 | fhx11 | fhx12 | fhx13 | fhx14 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 3 | 0 | 0 | 0 | . | 8 | 0 | 0 | 1 | 1 | 1 | . | 1 | 0 |
2 | 3 | 0 | 0 | 0 | . | 0 | 0 | 0 | 0 | 0 | 0 | . | 0 | 0 |
3 | 3 | 0 | 0 | 0 | . | 0 | 0 | 0 | 0 | 0 | 0 | . | 0 | 0 |
4 | 5 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 1 |
5 | 5 | 0 | 0 | 0 | 0 | 1 | 8 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
Obs | stat1 | stat2 | stat3 | stat4 | stat5 | stat6 | stat7 | stat8 | stat9 | stat10 | stat11 | stat12 | stat13 | stat14 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
2 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
3 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
The first ARRAY statement tells SAS to group the fourteen family history variables, fhx1, ..., fhx14, into a one-dimensional array called edit. The second ARRAY statement tells SAS to group the fourteen status variables, stat1, ..., stat14, into a one-dimensional array called status. The DO loop tells SAS to review the contents of the 14 variables and to assign each element of the status array a value of 0 ("status(i) = 0"). If the element of the edit array is missing, however, then SAS is told to change the element of the status array from a 0 to a 1 ("if edit(i) = . then status(i) = 1").
Launch and run the SAS program. Then, review the output from the PRINT procedures to convince yourself that the program does indeed do as described.
Example 19.19 Section
The previous program was written merely as a prelude to this example, in which the code is modified to illustrate the use of two-dimensional arrays. This program performs exactly the same task as the previous program, namely searching a subset of family history data for missing values. Here, though, we use one two-dimensional array called edit instead of two one-dimensional arrays:
DATA fhx2;
input subj v_date mmddyy8. fhx1-fhx14;
array edit(2,14) fhx1-fhx14 stat1-stat14;
do i = 1 to 14;
edit(2,i) = 0;
if edit(1,i) = . then edit(2,i) = 1;
end;
DATALINES;
220004 07/27/93 0 0 0 . 8 0 0 1 1 1 . 1 0 1
410020 11/11/93 0 0 0 . 0 0 0 0 0 0 . 0 0 0
520013 10/29/93 0 0 0 . 0 0 0 0 0 0 . 0 0 1
520068 08/10/95 0 0 0 0 0 1 1 0 0 1 1 0 1 0
520076 08/25/95 0 0 0 0 1 8 0 0 0 1 1 0 0 1
;
RUN;
PROC PRINT data = fhx2;
var fhx1-fhx14;
TITLE 'The FHX2 data itself';
RUN;
PROC PRINT data = fhx2;
var stat1-stat14;
TITLE 'The presence of missing values in FHX2 data';
RUN;
Obs | fhx1 | fhx2 | fhx3 | fhx4 | fhx5 | fhx6 | fhx7 | fhx8 | fhx9 | fhx10 | fhx11 | fhx12 | fhx13 | fhx14 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 3 | 0 | 0 | 0 | . | 8 | 0 | 0 | 1 | 1 | 1 | . | 1 | 0 |
2 | 3 | 0 | 0 | 0 | . | 0 | 0 | 0 | 0 | 0 | 0 | . | 0 | 0 |
3 | 3 | 0 | 0 | 0 | . | 0 | 0 | 0 | 0 | 0 | 0 | . | 0 | 0 |
4 | 5 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 1 |
5 | 5 | 0 | 0 | 0 | 0 | 1 | 8 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
Obs | stat1 | stat2 | stat3 | stat4 | stat5 | stat6 | stat7 | stat8 | stat9 | stat10 | stat11 | stat12 | stat13 | stat14 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
2 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
3 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
First, if you compare this program with the previous program, you should notice that the two programs have more similarities than differences. Here, we have just one ARRAY statement that defines the two-dimensional array edit containing 2 rows and 14 columns. The ARRAY statement tells SAS to group the family history variables (fhx1, ..., fhx14) into the first dimension and to group the status variables (stat1, ..., stat14) into the second dimension. Then, the DO loop tells SAS to review the contents of the 14 variables and to assign each element of the status dimension a value of 0 ("edit(2,i) = 0;"). If the element of the edit dimension is missing, however, then SAS is told to change the element of the status dimension from a 0 to a 1 ("if edit(1,i) = . then edit(2,i) = 1").
Launch and run the SAS program. Then, review the output from the PRINT procedures to convince yourself that the program accomplishes using one two-dimensional array just as the previous program accomplished using two one-dimensional arrays.