REF Method

Consolidates the FIND and ADD methods into a single method call.
Applies to: Hash object

Syntax

Arguments

rc
specifies whether the method succeeded or failed.
A return code of zero indicates success; a nonzero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.
object
specifies the name of the hash object.
KEY: keyvalue
specifies the key value whose type must match the corresponding key variable that is specified in a DEFINEKEY method call.
The number of “KEY: keyvalue” pairs depends on the number of key variables that you define by using the DEFINEKEY method.
DATA: datavalue
specifies the data value whose type must match the corresponding data variable that is specified in a DEFINEDATA method call.
The number of “DATA: datavalue” pairs depends on the number of data variables that you define by using the DEFINEDATA method.

Details

You can consolidate FIND and ADD methods into a single REF method. You can change the following code:
rc = h.find();
  if (rc ne = 0) then
    rc = h.add();
to
rc = h.ref();
The REF method is useful for counting the number of occurrences of each key in a hash object. The REF method initializes the key summary for each key on the first ADD, and then changes the ADD for each subsequent FIND.
Note: The REF method sets the data variable to the value of the data item so that it is available for use after the method call.
For more information about key summaries, see Maintaining Key Summaries in SAS Language Reference: Concepts.

Example: Using the REF Method for Key Summaries

The following example uses the REF method for key summaries:
data keys;
 input key;
datalines;
1
2
1
3
5
2
3
2
4
1
5
1
;
data count;
 length count key 8;
 keep key count;
 if _n_ = 1 then do;
   declare hash myhash(suminc: "count", ordered: "y");
   declare hiter iter("myhash");
   myhash.defineKey('key');
   myhash.defineDone();
   count = 1;
  end;
 do while (not done);
   set keys end=done;
   rc = myhash.ref();
 end;
 rc = iter.first();
 do while(rc = 0);
   rc = myhash.sum(sum: count);
   output;
   rc = iter.next();
 end;
 stop;
run;

proc print data=count;
run;
Output of DATA Using the REF Method
Output of COUNT using the REF Method

See Also