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 567