SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Processing Variables with Arrays
Creating One-Dimensional Arrays


Referencing Elements of an Array

Now let's look at some ways you can use arrays to process variables in the DATA step.

     data work.report;
        set master.temps;
        array wkday(7) mon tue wed thr fri sat sun;
        do i=1 to 7;
           if wkday(i)>95 then output;
        end;
     run;

                         data work.weights;
                            set master.class;
                            array wt(6) w1-w6;
                            do i=1 to 6;
                               wt(i)=wt(i)*2.2;
                            end;
                         run;

     data work.new;
        set master.synyms;
        array term(9) also1-also9;
        do i=1 to 9;
           if term(i) ne ' ' then output;
        end;
     run;

Referencing the elements of an array by an index value is what gives arrays their power. Typically, arrays are used with DO loops to process multiple variables and perform repetitive calculations.

     array qtr(4) jan apr jul oct;
     do i=1 to 4;
        YearGoal=qtr(i)*1.2;
     end;

When you define an array in a DATA step, each array element is assigned an index value. The index values are assigned in the order of the array elements.

                   1   2   3   4                          
     array qtr(4) jan apr jul oct;                        
     do i=1 to 4;                                         
        YearGoal=qtr(i)*1.2;                              
     end;

During execution, you can use an array reference to perform an action on an array element. To reference an array element in the DATA step, specify the name of the array followed by an index value enclosed in parentheses.


General form, ARRAY reference:
array-name(index value)

where index value

  • is enclosed in parentheses
  • specifies a variable, SAS expression, or integer
  • is within the lower and upper bounds of the dimension of the array.


When used in a DO loop, the index variable of the iterative DO statement can reference each element of the array.
     array qtr(4) jan apr jul oct;
     do i=1 to 4;
        YearGoal=qtr(i)*1.2;
     end;

For instance, the DO loop above increments the index variable i from the lower bound of the qtr array, 1, to the upper bound, 4. The following sequence illustrates this process:

                   1
     array qtr(4) jan apr jul oct;
     do i=1 to 4;
        YearGoal=qtr(1)*1.2;
     end;

                               2
             array qtr(4) jan apr jul oct;
             do i=1 to 4;
                YearGoal=qtr(2)*1.2;
             end;

                                           3
                     array qtr(4) jan apr jul oct;
                     do i=1 to 4;
                        YearGoal=qtr(3)*1.2;
                     end;

                                                       4
                             array qtr(4) jan apr jul oct;
                             do i=1 to 4;
                                YearGoal=qtr(4)*1.2;
                             end;

During each iteration of the DO loop, qtr(i) refers to an element of the array qtr in the order listed.


back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement