SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Processing Variables with Arrays
Expanding Your Use of Arrays


During the compilation of the DATA step, the variables created with this ARRAY statement are added to the program data vector and are stored in the resulting data set.
     data hrd.diff;
        set hrd.convert;
        array wt(6) weight1-weight6;
        array WgtDiff(5);

Program Data Vector

NOTE: When referencing the array elements, be careful not to confuse the array references WgtDiff(1) through WgtDiff(5) (note parentheses) with the variable names WgtDiff1 through WgtDiff5. The program data vector below shows the relationship between the array references and the corresponding variable names.


Program Data Vector

Now you can use a DO loop to calculate the differences between each of the recorded weights. Notice that each value of WgtDiff(i) is calculated by subtracting wt(i) from wt(i+1). By manipulating the index variable, you can easily reference any array element.
     data hrd.diff;
        set hrd.convert;
        array wt(6) weight1-weight6;
        array WgtDiff(5);
        do i=1 to 5;
           WgtDiff(i)=wt(i+1)-wt(i);
        end;
     run;

A portion of the resulting data set is shown below.


SAS Data Set Hrd.Diff
Name WgtDiff1 WgtDiff2 WgtDiff3 WgtDiff4 WgtDiff5
Alicia -1.54322 -0.22046 -3.08644 -3.08644 0.44092
Betsy 0.00000 -1.98414 -2.86598 -1.32276 -1.54322
Brenda -2.20460 -1.32276 -1.32276 -1.32276 -1.32276



back||next


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

Terms of Use & Legal Information | Privacy Statement