do i=lbound(years) to hbound(years);
if years{i}=99 then years{i}=.;
end;
The value of LBOUND(YEARS) is 72; the value of HBOUND(YEARS) is 76.
For this example, the DIM function would return a value of 5, the total count of elements
in the array YEARS. Therefore, if you used the DIM function instead of the HBOUND
function for the upper bound of the array, the statements inside the DO loop would not
have executed.
Specifying Bounds in a Two-Dimensional Array
The following list contains 40 variables named X60 through X99. They represent the
years 1960 through 1999.
X60 X61 X62 X63 X64 X65 X66 X67 X68 X69
X70 X71 X72 X73 X74 X75 X76 X77 X78 X79
X80 X81 X82 X83 X84 X85 X86 X87 X88 X89
X90 X91 X92 X93 X94 X95 X96 X97 X98 X99
The following ARRAY statement arranges the variables in an array by decades. The
rows range from 6 through 9, and the columns range from 0 through 9.
array X{6:9,0:9} X60-X99;
In array X, variable X63 is element X{6,3} and variable X89 is element X{8,9}. To
process array X with iterative DO loops, use one of these methods:
Method 1:
do i=6 to 9;
do j=0 to 9;
if X{i,j}=0 then X{i,j}=.;
end;
end;
Method 2:
do i=lbound1(X) to hbound1(X);
do j=lbound2(X) to hbound2(X);
if X{i,j}=0 then X{i,j}=.;
end;
end;
Both examples change all values of 0 in variables X60 through X99 to missing. The first
example sets the range of the DO groups explicitly. The second example uses the
LBOUND and HBOUND functions to return the bounds of each dimension of the array.
Examples of Array Processing
Example 1: Using Character Variables in an Array
You can specify character variables and their lengths in ARRAY statements. The
following example groups variables into two arrays, NAMES and CAPITALS. The
dollar sign ($) tells SAS to create the elements as character variables. If the variables
have already been declared as character variables, a dollar sign in the array is not
necessary. The INPUT statement reads all the variables in array NAMES.
Examples of Array Processing 577
The statement inside the DO loop uses the UPCASE function to change the values of the
variables in array NAMES to uppercase. The statement then stores the uppercase values
in the variables in the CAPITALS array.
options linesize=80 pagesize=60;
data text;
array names{*} $ n1-n5;
array capitals{*} $ c1-c5;
input names{*};
do i=1 to 5;
capitals{i}=upcase(names{i});
end;
datalines;
smithers michaels gonzalez hurth frank
;
proc print data=text;
title 'Names Changed from Lowercase to Uppercase';
run;
The following output shows the TEXT data set.
Output 25.3 Using Character Variables in an Array
Example 2: Assigning Initial Values to the Elements of an Array
This example creates variables in the array Test and assigns them the initial values 90,
80, and 70. It reads values into another array named Score and compares each element of
Score to the corresponding element of Test. If the value of the element in Score is greater
than or equal to the value of the element in Test, the variable NewScore is assigned the
value in the element Score. The OUTPUT statement writes the observation to the SAS
data set.
The INPUT statement reads a value for the variable named ID and then reads values for
all the variables in the Score array.
options linesize=80 pagesize=60;
data score1(drop=i);
array test{3} t1-t3 (90 80 70);
array score{3} s1-s3;
input id score{*};
do i=1 to 3;
if score{i}>=test{i} then
do;
NewScore=score{i};
output;
end;
578 Chapter 25 Array Processing
end;
datalines;
1234 99 60 82
5678 80 85 75
;
proc print noobs data=score1;
title 'Data Set SCORE1';
run;
The following output shows the Score1 data set.
Output 25.4 Assigning Initial Values to the Elements of an Array
Example 3: Creating an Array for Temporary Use in the Current
DATA Step
When elements of an array are constants that are needed only for the duration of the
DATA step, you can omit variables from an array group and instead use temporary array
elements. You refer to temporary data elements by the array name and dimension.
Although they behave like variables, temporary array elements do not have names, and
they do not appear in the output data set. Temporary array elements are automatically
retained, instead of being reset to missing at the beginning of the next iteration of the
DATA step.
To create a temporary array, use the _TEMPORARY_ argument. The following example
creates a temporary array named Test:
options linesize=80 pagesize=60;
data score2(drop=i);
array test{3} _temporary_ (90 80 70);
array score{3} s1-s3;
input id score{*};
do i=1 to 3;
if score{i}>=test{i} then
do;
NewScore=score{i};
output;
end;
end;
datalines;
Examples of Array Processing 579

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.