Warren Repole

Don't Be a SAS® Dinosaur: Modernize Your SAS Programs
by Warren Repole

Arrays: Temporary Elements


Scenario:

You want to perform a table lookup using an array of constants.

You can create the array by assigning initial values to the elements, but the underlying variables appear in the output data set by default.


The old way: Drop the Lookup Array Elements

The original approach is to eliminate the array elements from the output data set using the DROP statement or the DROP= data set option.

Download this program
data Differences(drop=i);
   set sashelp.class;
   array BaseLine {3} (13.32 62.33 100.03);
   drop BaseLine1-BaseLine3 ;
   array Actuals {3} Age Height Weight;
   array Diffs {3} DiffAge DiffHgt DiffWgt;
   do i = 1 to 3 ;
      Diffs{i} = Actuals{i} - BaseLine{i};
   end;
run;
proc print data=Differences;
  title1 "Differences from BaseLine";
run;

SAS Log

20893  data Differences(drop=i);
20894     set sashelp.class;
20895     array BaseLine {3} (13.32 62.33 100.03);
20896     drop BaseLine1-BaseLine3 ;
20897     array Actuals {3} Age Height Weight;
20898     array Diffs {3} DiffAge DiffHgt DiffWgt;
20899     do i = 1 to 3 ;
20900        Diffs{i} = Actuals{i} - BaseLine{i};
20901     end;
20902  run;
 
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.DIFFERENCES has 19 observations and 8 variables.
 
20903  proc print data=Differences;
20904    title1 "Differences from BaseLine";
20905  run;
 
NOTE: There were 19 observations read from the data set WORK.DIFFERENCES.
 

SAS Listing Output

Differences from BaseLine
 
                                                     Diff      Diff      Diff
Obs    Name       Sex    Age    Height    Weight     Age        Hgt       Wgt
 
  1    Alfred      M      14     69.0      112.5     0.68      6.67     12.47
  2    Alice       F      13     56.5       84.0    -0.32     -5.83    -16.03
  3    Barbara     F      13     65.3       98.0    -0.32      2.97     -2.03
  4    Carol       F      14     62.8      102.5     0.68      0.47      2.47
  5    Henry       M      14     63.5      102.5     0.68      1.17      2.47
  6    James       M      12     57.3       83.0    -1.32     -5.03    -17.03
  7    Jane        F      12     59.8       84.5    -1.32     -2.53    -15.53
  8    Janet       F      15     62.5      112.5     1.68      0.17     12.47
  9    Jeffrey     M      13     62.5       84.0    -0.32      0.17    -16.03
 10    John        M      12     59.0       99.5    -1.32     -3.33     -0.53
 11    Joyce       F      11     51.3       50.5    -2.32    -11.03    -49.53
 12    Judy        F      14     64.3       90.0     0.68      1.97    -10.03
 13    Louise      F      12     56.3       77.0    -1.32     -6.03    -23.03
 14    Mary        F      15     66.5      112.0     1.68      4.17     11.97
 15    Philip      M      16     72.0      150.0     2.68      9.67     49.97
 16    Robert      M      12     64.8      128.0    -1.32      2.47     27.97
 17    Ronald      M      15     67.0      133.0     1.68      4.67     32.97
 18    Thomas      M      11     57.5       85.0    -2.32     -4.83    -15.03
 19    William     M      15     66.5      112.0     1.68      4.17     11.97
bronto

The new way: Create the Array with Temporary Elements
(available in SAS Version 6)

An alternate approach is to specify the array to contain temporary elements.

Download this program
data Differences(drop=i);
   set sashelp.class;
   array BaseLine {3} _temporary_ (13.32 62.33 100.03);
   array Actuals {3} Age Height Weight;
   array Diffs {3} DiffAge DiffHgt DiffWgt;
   do i = 1 to 3 ;
      Diffs{i} = Actuals{i} - BaseLine{i};
   end;
run;
proc print data=Differences;
  title1 "Differences from BaseLine";
run;

SAS Log

20917  data Differences(drop=i);
20918     set sashelp.class;
20919     array BaseLine {3} _temporary_ (13.32 62.33 100.03);
20920     array Actuals {3} Age Height Weight;
20921     array Diffs {3} DiffAge DiffHgt DiffWgt;
20922     do i = 1 to 3 ;
20923        Diffs{i} = Actuals{i} - BaseLine{i};
20924     end;
20925  run;
 
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.DIFFERENCES has 19 observations and 8 variables.
 
20926  proc print data=Differences;
20927    title1 "Differences from BaseLine";
20928  run;
 
NOTE: There were 19 observations read from the data set WORK.DIFFERENCES.
 

SAS Listing Output

Same as for the old way


Advantages of the alternate approach: Good

  • No additional statements or data set options are needed in order to prevent the array elements from appearing in the output data set.

Disadvantages of the alternate approach: Bad

  • The number of elements must be declared explicitly in the ARRAY statement. [Lower and upper bounds are permitted.]

Additional documentation for this technique can be found in SAS® 9.2 Language Reference: Dictionary. Cary, NC: SAS Institute Inc.

Visit http://support.sas.com/documentation/onlinedoc/sas9doc.html for SAS 9 documentation.

The URL for this page is http://www.repole.com/dinosaur/temparray.html

These techniques are mentioned in other SAS references and publications:


Back to SAS Dinosaur home page

Printable copy of this page (without sample output)

SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries.

® indicates USA registration.