Previous Page | Next Page

SAS Component Language Dictionary

ARRAY



Defines elements of an explicit array
Category: Declarative Statement
Comparisons: SAS Statement with limitations in SCL

Syntax
Details
Reference Array
Differences from DATA Step in ARRAY Statement Execution
Examples
Example 1: Using Repetition Factors for Array Initialization
Example 2: Using an Array with the IN Operator
Example 3: Using a Reference Array with a METHOD Statement
See Also

Syntax

ARRAY array-name<{n}><$><length><elements>
<(initial-values)>;

array-name

is the name of the array. It cannot be the same as the name of a window variable. However, window variables can be elements of an array.

Type: Character

{n}

is either the dimension of the array, or an asterisk (*) to indicate that the dimension is determined from the number of array elements or initial values. Multidimensional arrays are allowed. If an asterisk is specified without any array elements or initial values, then the array is a reference array. The dimension of this array will be determined at execution time, based on the corresponding array in the calling program.

Type: Numeric

$

indicates that the array type is character.

length

is the maximum length of elements in the array. For character arrays, the maximum length cannot exceed 200. The default length is 8 characters. Length is ignored for numeric arrays.

Type: Numeric

elements

are the variables (either window or nonwindow variables) that make up the array, or you can specify '_TEMPORARY_' to create a list of temporary data elements.

Type: Character

initial-values

are the values to use to initialize some or all of the array elements. Separate these values with commas or blanks. By default, all the elements in an array are initialized to missing.

Type: Character


Details

If you have elements that you reference only with subscripting, then you can save memory by using the _TEMPORARY_ keyword. The SCL compiler has to allocate memory for the array name and the names of the array elements. However, if this keyword is used, the compiler allocates memory only for the array name. For large arrays, this could result in significant memory savings.

Note:   Do not use '_TEMPORARY_' for elements if you plan to use the SET routine to fetch values from a SAS table directly into an array. Use GETVARN and GETVARC when '_TEMPORARY_' is specified.  [cautionend]

Note:   You can also declare temporary arrays using the DECLARE statement.  [cautionend]


Reference Array

A reference array is a pointer to another defined array. Previously, when an array needed to be passed as a parameter to a METHOD or ENTRY statement, an array of equal size needed to be defined in both the calling program and the called program. This technique used twice as much memory as was actually required. With reference arrays, only one array needs to be defined with the actual size. The array in the called program uses the actual memory of the array in the calling program.

By using reference arrays, you can create general array functions, because the array dimension is determined by the calling program. That is, you do not need to hardcode the array dimension in the SCL program that contains the ENTRY or METHOD statement. See the example later in this section for an illustration of this concept.

Using multidimensional reference arrays is allowed when the dimensions match. For example, if a two-dimensional array is passed in, the reference array must also be two-dimensional.

Reference arrays can currently be used as parameters only in a METHOD or ENTRY statement. Once a reference array has been created by a call to another program, it can be used in any way that a regular array can be used.


Differences from DATA Step in ARRAY Statement Execution

The ARRAY statement in SCL is very similar to the ARRAY statement in the DATA step and is used to define single or multidimensional arrays. The ARRAY statement in SCL differs from the DATA step ARRAY statement in the following ways:

For details about the ARRAY statement in the Base SAS language, see SAS Language Reference: Dictionary.


Examples


Example 1: Using Repetition Factors for Array Initialization

In the following statement, note that 1 is repeated three times and the pattern 2,3,4 is repeated four times:

array a{16}(0,3*1 ,4*(2,3,4));

This statement initializes the values of the elements of array A as follows:

0, 1, 1, 1, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4


Example 2: Using an Array with the IN Operator

Consider the following code segment:

array a 8 (2 4 6 8 10);

INIT:
   b=6;
   if b in a then put 'B is in array A';
      /* returns location of B in array A */
   c=b in a;
   put c=;
return;

This code produces the following output:

B is in array A
C=3


Example 3: Using a Reference Array with a METHOD Statement

Assume that an entry SORT.SCL contains the method definition shown below. The method illustrates the use of a reference array to define a generic sort routine. The routine is termed generic because NSORT does not need to know the size of the array that is being passed: the reference array NARRAY takes on the definition of the array that is specified in the CALL METHOD routine.

nsort: method narray [*]:num;
   size = dim( narray );
   do i = 1 to size - 1;
      do j = i + 1 to size;
         if narray( i ) > narray( j ) then
            do;
               ntemp = narray( i );
               narray( i ) = narray( j );
               narray( j ) = ntemp;
            end;
      end;
   end;
endmethod;

Here is a sample calling program that executes the NSORT method:

array numarray(100);

MAIN:
  do i=1 to dim(numarray);
     numarray(i)=dim(numarray)-i+1;
  end;
  call method('sort.scl', 'nsort', numarray);
return;


See Also

DECLARE

Previous Page | Next Page | Top of Page