Expanding Your Use of Arrays

Creating Variables in an ARRAY Statement

Previous examples showed several ways to reference existing variables in an ARRAY statement. You can also create variables in an ARRAY statement by omitting the array elements from the statement. If you do not reference existing variables, SAS automatically creates new variables for you and assigns default names to them. The default name is the array-name followed by consecutive numbers 1 to the dimension of the array.
Syntax, ARRAY statement to create new variables:
ARRAY array-name{dimension};
  • array-name specifies the name of the array.
  • dimension describes the number and arrangement of array elements.
For example, suppose you need to calculate the weight gain or loss from week to week for each member of a fitness class, shown below.
Figure 16.2 SAS Data Set Hrd.Convert
SAS Data Set Hrd.Convert
You would 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;
Next, you want to create the new variables to store the differences between the six recorded weights. You can use an additional ARRAY statement without elements to create the new variables, WgtDiff1 to WgtDiff5.
data hrd.diff; 
   set hrd.convert; 
   array wt{6} weight1-weight6; 
   array WgtDiff{5};
run;
proc print data=hrd.convert;
run;
Remember, when creating variables in an ARRAY statement, you do not need to specify array elements as long as you specify how many elements are in the array.
array WgtDiff{5};

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
Tip
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

Creating 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 that are created in 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 that you specify is automatically assigned to all variables that are created by the ARRAY statement.
During the compilation of the DATA step, the variables that this ARRAY statement creates 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 the braces) 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; 
proc print data=hrd.diff;
run;
A portion of the resulting data set is shown below.
Figure 16.3 HTML Output: SAS Data Set Hrd.Diff (partial output)
SAS Data Set Hrd.Diff

Assigning Initial Values to Arrays

It is useful to assign initial values to elements of an array when you define the array.
array goal{4} g1 g2 g3 g4 (initial values);
To assign initial values in an ARRAY statement:
  1. Place the values after the array elements.
    array goal{4} g1 g2 g3 g4 (9000 9300 9600 9900);
  2. Specify one initial value for each corresponding array element.
                          
    array goal{4} g1 g2 g3 g4 (9000 9300 9600 9900);
                                            
  3. Separate each value with a comma or blank.
                                            
    array goal{4} g1 g2 g3 g4 (9000 9300 9600 9900);
  4. Enclose the initial values in parentheses.
                                                 
    array goal{4} g1 g2 g3 g4 (9000 9300 9600 9900);
  5. Enclose each character value in quotation marks.
                                                 
    array col{3} $ color1-color3 ('red','green','blue');
It is also possible to assign initial values to an array without specifying each array element. The following statement creates the variables Var1, Var2, Var3, and Var4, and assigns them initial values of 1, 2, 3, and 4:
array Var{4} (1 2 3 4);
For this example, assume that you have the task of comparing the actual sales figures in the Finance.Qsales data set to the sales goals for each sales representative at the beginning of the year. The sales goals are not recorded in Finance.Qsales.
Figure 16.4 Description of Finance.Qsales
Description of Finance.Qsales
The DATA step below reads the Finance.Qsales data set to create the Finance.Report data set. The ARRAY statement creates an array to process sales data for each quarter.
data finance.report; 
   set finance.qsales; 
   array sale{4} sales1-sales4;
To compare the actual sales to the sales goals, you must create the variables for the sales goals and assign values to them.
data finance.report; 
   set finance.qsales; 
   array sale{4} sales1-sales4; 
   array Goal{4} (9000 9300 9600 9900);
A third ARRAY statement creates the variables Achieved1 through Achieved4 to store the comparison of actual sales versus sales goals.
data finance.report; 
   set finance.qsales; 
   array sale{4} sales1-sales4; 
   array Goal{4} (9000 9300 9600 9900); 
   array Achieved{4}; 
   do i=1 to 4; 
      achieved{i}=100*sale{i}/goal{i}; 
   end; 
run;
A DO loop executes four times to calculate the value of each element of the achieved array (expressed as a percentage).
data finance.report; 
   set finance.qsales; 
   array sale{4} sales1-sales4; 
   array Goal{4} (9000 9300 9600 9900); 
   array Achieved{4}; 
   do i=1 to 4; 
      achieved{i}=100*sale{i}/goal{i}; 
   end; 
run;
Before submitting this DATA step, you can drop the index variable from the new data set by adding a DROP= option to the DATA statement.
data finance.report(drop=i); 
   set finance.qsales; 
   array sale{4} sales1-sales4; 
   array Goal{4} (9000 9300 9600 9900); 
   array Achieved{4}; 
   do i=1 to 4; 
      achieved{i}=100*sale{i}/goal{i}; 
   end;
run; 
proc print data=finance.report;
run;
This is an example of a simple table-lookup program. The resulting data set contains the variables that were read from Finance.Qsales, plus the eight variables that were created with ARRAY statements.
Figure 16.5 HTML Output: SAS Finance.Report
SAS Data Set Finance.Report
Note: Variables to which initial values are assigned in an ARRAY statement are automatically retained.
The variables Goal1 through Goal4 should not be stored in the data set because they are needed only to calculate the values of Achieved1 through Achieved4.

Creating Temporary Array Elements

To create temporary array elements for DATA step processing without creating new variables, specify _TEMPORARY_ after the array name and dimension.
data finance.report(drop=i); 
   set finance.qsales; 
   array sale{4} sales1-sales4; 
   array goal{4} _temporary_ (9000 9300 9600 9900); 
   array Achieved{4}; 
   do i=1 to 4; 
      achieved{i}=100*sale{i}/goal{i}; 
   end; 
run;
proc print data=finance.report noobs;
run;
Temporary array elements do not appear in the resulting data set.
Figure 16.6 SAS Data Set Finance.Report
SAS Data Set Finance.Report
Tip
Temporary array elements are useful when the array is needed only to perform a calculation. Use them to improve performance time.
Last updated: January 10, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.206.69