Send As SMS

Thursday, March 22, 2007

Simple example using "hash" object (associative array)

This is a simple example of the use of the "hash" or associative array object in the SAS version 9 data step. This example doesn't accomplish anything except demonstrate the syntax.

data test;
   length outname $5.;
   do outname = 'one', 'two', 'three';
      do i = 1 to 5;
         random = ranuni(95605);
         output;
      end;
   end;
run;

data _null_;

   if _n_ = 1 then 
      do;
      length outname $5.;
      length i random 8.;
      /* Read TEST data set */
      declare hash test(dataset: 'test');
      /* Define OUTNAME as key and I, RANDOM as data for hash object */
      test.defineKey('outname', 'i', 'random');
      test.defineData('outname', 'i', 'random');
      test.defineDone();
      /* avoid uninitialized variable notes */
      call missing(outname, i, random);
      end;

   /* Create output data set from hash object */
   rc = test.output(dataset: 'work.out');
   stop;

*****; run;

proc print data=work.out;
run;

prints

Obs    outname    i     random

  1     three     2    0.64121
  2     one       4    0.80749
  3     three     1    0.95194
  4     one       2    0.33166
  5     three     3    0.76309
  6     one       5    0.81446
  7     two       5    0.09517
  8     one       3    0.20278
  9     three     4    0.18146
 10     two       1    0.76102
 11     two       3    0.38716
 12     three     5    0.26591
 13     two       4    0.55788
 14     one       1    0.34649
 15     two       2    0.52385

0 Comments:

Post a Comment

<< Home