number-of-elements
is the number of variables in the group. You must enclose this value in either
parentheses (), braces {}, or brackets [].
$
specifies that the elements in the array are character elements.
length
specifies the length of the elements in the array that have not been previously
assigned a length.
array-elements
is a list of the names of the variables in the group. All variables that are defined in a
given array must be of the same type, either all character or all numeric.
initial-value-list
is a list of the initial values for the corresponding elements in the array.
For complete information, see the “ARRAY Statement” in SAS DATA Step Statements:
Reference.
To reference an array that was previously defined in the same DATA step, use an Array
Reference statement. An array reference has the following form:
array-name {subscript}
where
array-name
is the name of an array that was previously defined with an ARRAY statement in the
same DATA step.
subscript
specifies the subscript, which can be a numeric constant, the name of a variable
whose value is the number, a SAS numeric expression, or an asterisk (*).
Note: Subscripts in SAS are 1-based by default, and not 0-based as they are in some
other programming languages.
For complete information, see the Array Reference statement in the SAS DATA Step
Statements: Reference.
Processing Simple Arrays
Grouping Variables in a Simple Array
The following ARRAY statement creates an array named Books that contains the three
variables Reference, Usage, and Introduction:
array books{3} Reference Usage Introduction;
When you define an array, SAS assigns each array element an array reference with the
form array-name{subscript}, where subscript is the position of the variable in the list.
The following table lists the array reference assignments for the previous ARRAY
statement:
568 Chapter 25 Array Processing
Table 25.1 Array Reference Assignments for Array Books
Variable Array Reference
Reference books{1}
Usage books{2}
Introduction books{3}
Later in the DATA step, when you want to process the variables in the array, you can
refer to a variable by either its name or its array reference. For example, the names
Reference and Books{1} are equivalent.
Using a DO Loop to Repeat an Action
To perform the same action several times, use an iterative DO loop. A simple iterative
DO loop that processes an array has the following form:
DO index-variable=1 TO number-of-elements-in-array;
… more SAS statements …
END;
The loop is processed repeatedly (iterates) according to the instructions in the iterative
DO statement. The iterative DO statement contains an index-variable whose name you
specify and whose value changes at each iteration of the loop.
To execute the loop as many times as there are variables in the array, specify that the
values of index-variable are 1 TO number-of-elements-in-array. SAS increases the value
of index-variable by 1 before each new iteration of the loop. When the value exceeds the
number-of-elements-in-array, SAS stops processing the loop. By default, SAS
automatically includes index-variable in the output data set. Use a DROP statement or
the DROP= data set option to prevent the index variable from being written to your
output data set.
An iterative DO loop that executes three times and has an index variable named count
has the following form:
do count=1 to 3;
more SAS statements
end;
The first time that the loop processes, the value of count is 1; the second time, 2; and the
third time, 3. At the beginning of the fourth iteration, the value of count is 4, which
exceeds the specified range and causes SAS to stop processing the loop.
Using a DO Loop to Process Selected Elements in an Array
To process particular elements of an array, specify those elements as the range of the
iterative DO statement. For example, the following statement creates an array Days that
contains seven elements:
array days{7} D1-D7;
The following DO statements process selected elements of the array Days:
Processing Simple Arrays 569

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.