Chapter Contents


SAS Component Language: Reference

Initializing The Elements of A Static Array

By default, all elements in a numeric array are initialized to numeric missing values if the array elements did not previously exist.

You can define initial values for the elements of a static array by listing the initial values in parentheses following the list of element names in the DECLARE or ARRAY statements. Commas are optional between variable values. For example, the following ARRAY statement creates a two-item array named COUNT, assigns the value 1 to the first element, and assigns the value 2 to the second element:

array count[2] (1 2);

You can also initialize array elements with the DECLARE statement. For example, the following program declares an array named MONTH, which contains five elements that can each contain three characters, and it assigns initial values to the array elements:

declare char(3) month[5]=('jan' 'feb' 'mar'
                           'apr' 'may');
   put month;
The example produces the following output:
month[1] = 'jan'
month[2] = 'feb'
month[3] = 'mar'
month[4] = 'apr'
month[5] = 'may'

Assigning the Same Value to Multiple Elements

You can use repetition factors to initialize static arrays. Repetition factors specify how many times the values are assigned in the array. They have the following form:

5 * (2 3 4)
In this example, 5 is the repetition factor and (2 3 4) is the list of initial values for the array elements. If the list consists of only a single item, then you can omit the parentheses.

For example, the following ARRAY and DECLARE statements both use repetition factors to initialize the values of the array REPEAT:

array repeat[17] (0,3*1,4*(2,3,4),0);
declare num repeat[17]=(0,3*1,4*(2,3,4),0);
This example repeats the value 1 three times and the sequence 2, 3, 4 four times. The following values are assigned to the elements of the array REPEAT:
0, 1, 1, 1, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 0

Initializing Static Multidimensional Arrays

To initialize a static multidimensional array, use the ARRAY or DECLARE statement to list values for the first row of the array, followed by values for the second row, and so on. The following examples both initialize a two-dimensional array named COUNT with two rows and three columns:

array count[2,3] (1 2 3 4 5 6);

dcl num count[2,3]=(1 2 3 4 5 6);

Elements of the COUNT Array shows the values of the elements of this array.

Elements of the COUNT Array


For more information about arrays, see ARRAY and DECLARE.

Chapter Contents



Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.