SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Processing Variables with Arrays
Expanding Your Use of Arrays


Creating Variables with the ARRAY Statement

So far, you have learned several ways to reference existing variables using an ARRAY statement. You can also create variables using an ARRAY statement.

SAS Data Set Hrd.Convert
Name Weight1 Weight2 Weight3 Weight4 Weight5 Weight6
Alicia 153.4 151.9 151.7 148.6 145.5 145.9
Betsy 116.0 116.0 114.0 111.1 109.8 108.2
Brenda 151.2 149.0 147.7 146.4 145.1 143.7
Carl 149.0 146.8 145.5 144.2 142.9 141.5
Carmela 140.2 137.8 136.5 135.4 134.0 128.3


Assume you need to calculate the weight gain or loss from week to week for each member of a fitness class, shown above. You'd like to create variables that contain this weekly difference. To perform the calculation, you first group the variables Weight1 through Weight6 into an array.
     data hrd.diff;
        set hrd.convert;
        array wt(6) weight1-weight6;

An additional ARRAY statement enables you to create the new variables. For this example, you need five variables to store the differences between the six recorded weights.

     data hrd.diff;
        set hrd.convert;
        array wt(6) weight1-weight6;
        array WgtDiff(5);

SAS Data Set Hrd.Convert
Name Weight1 Weight2 Weight3 Weight4 Weight5 Weight6
Alicia 153.4 <-1->151.9 <-2->151.7 <-3->148.6 <-4->145.5 <-5->145.9
Betsy 116.0 116.0 114.0 111.1 109.8 108.2
Brenda 151.2 149.0 147.7 146.4 145.1 143.7
Carl 149.0 146.8 145.5 144.2 142.9 141.5
Carmela 140.2 137.8 136.5 135.4 134.0 128.3


When creating variables with an ARRAY statement, you do not need to specify array elements. Because you are not referencing existing variables, SAS software automatically creates the variables for you.
     array WgtDiff(5);

Arrays of Character Variables

To create an array of character variables, add a dollar sign ($) after the array dimension.

     array firstname(5) $;

By default, all character variables created with an ARRAY statement are assigned a length of 8. You can assign your own length by specifying the length after the dollar sign.

     array firstname(5) $ 24;

The length you specify is automatically assigned to all variables created by the ARRAY statement.


Default Variable Names

The default variable names are created by concatenating the array name and the numbers 1, 2, 3, and so on, up to the array dimension.

                   array WgtDiff(5);
                         . . . . .
                      .   .  .  .   .
                   .     .   .   .     .
                .       .    .    .       .
             .         .     .     .         .
          .           .      .      .           .
     WgtDiff1  WgtDiff2  WgtDiff3  WgtDiff4  WgtDiff5

Note: If you prefer, you can specify individual variable names. To specify variable names, you list each name as an element of the array. The following ARRAY statement creates the numeric variables Oct12, Oct19, Oct26, Nov02, and Nov09.

     array WgtDiff(5) Oct12 Oct19 Oct26 Nov02 Nov09;

                      array WgtDiff(5);
                            . . . . .
                          .  .  .  .  .
                        .   .   .   .   .
                      .    .    .    .    .
                    .     .     .     .     .
                  .      .      .      .      .
                Oct12  Oct19  Oct26  Nov02  Nov09


  back||next


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

Terms of Use & Legal Information | Privacy Statement