Array Reference Statement

Describes the elements in an array to be processed.

Valid in: DATA step
Categories: CAS
Information
Type: Declarative
Restriction: Variables with a VARCHAR data type are not supported.

Syntax

Arguments

array-name

is the name of an array that was previously defined with an ARRAY statement in the same DATA step.

{subscript}

specifies the subscript. Any of these forms can be used:

{variable-1< , …variable-n>}

specifies a variable or variable list that is usually used with DO-loop processing. For each execution of the DO loop, the current value of this variable becomes the subscript of the array element that is being processed.

Tip You can enclose a subscript in braces ( { } ), brackets ( [ ] ), or parentheses (( )).
Using Iterative DO-Loop Processing

{*}

forces SAS to treat the elements in the array as a variable list.

Restriction When you define an array that contains temporary array elements, you cannot reference the array elements with an asterisk.
Tips The asterisk can be used with the INPUT and PUT statements, and with some SAS functions.
This syntax is provided for convenience and is an exception to usual array processing.
Using the Asterisk References as a Variable List

expression-1< , ...expression-n>

specifies a SAS expression.

Range The expression must evaluate to a subscript value when the statement that contains the array reference executes. The expression can also be an integer with a value between the lower and upper bounds of the array, inclusive.
Specifying the Subscript

Details

  • To refer to an array in a program statement, use an array reference. The ARRAY statement that defines the array must appear in the DATA step before any references to that array. An array definition is in effect only for the duration of the DATA step. If you want to use the same array in several DATA steps, redefine the array in each step.

    CAUTION

    Using the name of a SAS function as an array name can cause unpredictable results. If you inadvertently use a function name as the name of the array, SAS treats parenthetical references that involve the name as array references, not function references, for the duration of the DATA step. A warning message is written to the SAS log.

  • You can use an array reference anywhere that you can write a SAS expression, including SAS functions and these SAS statements:
    • assignment statement
    • sum statement
    • DO UNTIL(expression)
    • DO WHILE(expression)
    • IF
    • INPUT
    • PUT
    • SELECT
    • WINDOW
  • The DIM function is often used with the iterative DO statement to return the number of elements in a dimension of an array, when the lower bound of the dimension is 1. If you use DIM, you can change the number of array elements without changing the upper bound of the DO statement. For example, because DIM(NEW) returns a value of 4, these statements process all the elements in the array:
    array new{*} score1-score4;
       do i=1 to dim(new);
          new{i}=new{i}+10;
       end;
  • You can use the IN operator to determine whether a value exists in an array. For example:
    data test;
    array v[3] (1 2 3);
    if 2 in v then flag = 5;
    run;

Comparisons

An ARRAY statement defines an array, whereas an array reference defines the members of the array to process.

Examples

Example 1: Using Iterative DO-Loop Processing

In this example, the statements process each element of the array, using the value of variable I as the subscript on the array references for each iteration of the DO loop. If an array element has a value of 99, the IF-THEN statement changes that value to 100.


data test;
   input d1 d2 d3 d4 d5 d6 d7;
   datalines;
1 2 99 3 99 0 99
;
run;

data test;
   set test;
   array days{7} d1-d7;
   do i=1 to 7;
      if days{i}=99 then days{i}=100;
   end;
   put d1= d2= d3= d4= d5= d6= d7=;
run;

SAS writes the following output to the log:

d1=1 d2=2 d3=100 d4=3 d5=100 d6=0 d7=100

Example 2: Referencing Many Arrays in One Statement

You can refer to more than one array in a single SAS statement. In this example, you create two arrays, DAYS and HOURS. The statements inside the DO loop substitute the current value of variable I to reference each array element in both arrays.

data test;
   input d1 d2 d3 d4 d5 d6 d7;
   datalines;
1 2 99 3 99 0 99
;
run;

data test;
   set test;
   array days{7} d1-d7;
   array hours{7} h1-h7;
   do i=1 to 7;
      if days{i}=99 then days{i}=100;
      hours{i}=days{i}*24;
   end;
   put d1= d2= d3= d4= d5= d6= d7=;
   put h1= h2= h3= h4= h5= h6= h7=;
run;

SAS writes the following output to the log:

d1=1 d2=2 d3=100 d4=3 d5=100 d6=0 d7=100
h1=24 h2=48 h3=2400 h4=72 h5=2400 h6=0 h7=2400

Example 3: Specifying the Subscript

In this example, the INPUT statement reads in variables A1, A2, and the third element (A3) of the array named ARR1.

data test;
   array arr1{*} a1-a3;
   x=1;
   input a1 a2 arr1{x+2};
    
   datalines;
1 2 3
;
run;

Example 4: Using the Asterisk References as a Variable List

  • data test;
       input cost1 cost2 cost3 cost4 cost5 cost6 cost7 cost8 cost9 cost10;
       datalines;
    10 20 30 40 50 60 70 80 90 100
    ;
    run;
    
    data test;
       set test;
       array cost{10} cost1-cost10;
       totcost=sum(of cost {*});
       put totcost=;
    run;
    
  • data test;
       array days{7} d1-d7;
       input days {*};
       datalines;
    1 2 3 4 5 6 7
    ;
    run;
    
  • data test;
       input h1 h2 h3 h4 h5 h6 h7;
       datalines;
    1 2 3 4 5 6 7
    ;
    run;
    
    data test;
       set test;
       array hours{7} h1-h7;
       put hours {*};
    run;
    

See Also

Last updated: June 17, 2025