Chapter Contents


SAS Component Language: Reference

Referencing Array Elements

To reference array elements, you can use the form array-name[position], where position is the index position of the variable in the array. This form of array reference is called subscripting. Subscripting is the only way to refer to array elements that were declared with the DECLARE statement. For example, FACTOR[4] is the only way to reference the fourth element of array FACTOR if it is created with the statement

 declare num Factor[5];
This DECLARE statement also produces variables FACTOR[1] through FACTOR[5].

Because you must use the DECLARE statement to declare dynamic arrays, the only way to reference the elements of a dynamic array is with subscripting. However, you cannot reference the elements of a dynamic array until you have created the array. See Creating and Initializing Dynamic Arrays for more information.

You can also use subscripting to refer to elements of an array that is declared with the ARRAY statement. For example, you can use MONTH[1] and MONTH[4] to refer to the first and fourth elements of an array that is declared with the following statement:

array month[5] $;

If the array is declared with an ARRAY statement that does not assign individual names to the array elements (as shown in this example), then you can also refer to these array elements as MONTH1 and MONTH4.

If the ARRAY statement assigns names to the individual array elements, then you can also use those names to refer to the array elements. For example, if you declare your array with the following statement, then you can refer to the elements in the array using the names JAN, FEB, and MAR:

array month[3] $ jan feb mar;

Grouping Variables That Have Sequential Names

If an application program or window has a series of variables whose names end in sequential numbers (for example, SCORE1, SCORE2, SCORE3, and so on), then you can use an array to group these variables. For example, the following ARRAY statement groups the variables SCORE1, SCORE2, SCORE3, and SCORE4 into the array SCORE:

array score[4];

Note:   If the variables do not already exist as window variables, then SCL defines new, nonwindow, numeric variables with those names.  [cautionend]

Grouping the variables into an array is useful when your program needs to apply the same operations to all of the variables. See Repeating an Action for Variables in an Array for more information.

Chapter Contents



Top of Page

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