Using Variable Lists to Define an Array Quickly
SAS reserves the following three names for use as variable list names:
_CHARACTER_
_NUMERIC_
_ALL_
You can use these variable list names to reference variables that have been previously
defined in the same DATA step. The _CHARACTER_ variable lists character values
only. The _NUMERIC_ variable lists numeric values only. The _ALL_ variable lists
either all character or all numeric values, depending on how you previously defined the
variables.
For example, the following INPUT statement reads in variables X1 through X3 as
character values using the $8. informat, and variables X4 through X5 as numeric
variables. The following ARRAY statement uses the variable list _CHARACTER_ to
include only the character variables in the array. The asterisk indicates that SAS
determines the subscript by counting the variables in the array.
input (X1-X3) ($8.) X4-X5;
array item {*} _character_;
You can use the _NUMERIC_ variable in your program (for example, you need to
convert currency). In this application, you do not need to know the variable names. You
need only to convert all values to the new currency.
For more information about variable lists, see the “ARRAY Statement” in SAS DATA
Step Statements: Reference.
Multidimensional Arrays: Creating and
Processing
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
Multidimensional Arrays: Creating and Processing 573
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:
Table 25.3 Array References for Array TEMPRG
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. For 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.
The outer DO loop (DO I=1 TO 2) processes the inner DO loop twice.
The inner DO loop (DO J=1 TO 5) applies the ROUND function to all the variables
in one row.
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 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;
574 Chapter 25 Array Processing

Get SAS 9.4 Language Reference, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.