Variations on Basic Array Processing

Determining the Number of Elements in an Array Efficiently

The DIM function in the iterative DO statement returns the number of elements in a one-dimensional array or the number of elements in a specified dimension of a multidimensional array, when the lower bound of the dimension is 1. Use the DIM function to avoid changing the upper bound of an iterative DO group each time you change the number of elements in the array.
The form of the DIM function is as follows:
DIMn(array-name)
where n is the specified dimension that has a default value of 1.
You can also use the DIM function when you specify the number of elements in the array with an asterisk. Here are some examples of the DIM function:
  • do i=1 to dim(days);
  • do i=1 to dim4(days) by 2;

DO WHILE and DO UNTIL Expressions

Arrays are often processed in iterative DO loops that use the array reference in a DO WHILE or DO UNTIL expression. In this example, the iterative DO loop processes the elements of the array named TREND.
data test;
   array trend{5} x1-x5;
   input x1-x5 y;
   do i=1 to 5 while(trend{i}<y);
   … more SAS statements … 
   end;
   datalines;
… data lines …
;

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 Statements: Reference.