REF Method

Consolidates the CHECK and ADD methods into a single method call.

Applies to:Hash object
Restriction:This method is available starting with the 2024.11 release.

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.

RestrictionThe keyvalue cannot contain a component object.

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.

RestrictionThe datavalue cannot contain a component object.

Details

You can consolidate CHECK and ADD methods into a single REF method. You can change the following code:

rc = h.check();
  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 CHECK.

For more information about key summaries, see Maintain Key Summaries.

Example: Using the REF Method for Key Summaries

The following example uses the REF method for key summaries:

data keys;
 input key_id;
datalines;
1
2
1
3
5
2
3
2
4
1
5
1
;

proc fcmp data=keys;
file log;
 length count key_id 8;
 if _n_ = 1 then do;
   declare hash myhash(suminc: "count", ordered: "y");
   declare hiter iter("myhash");
   rc = myhash.defineKey('key_id');
   rc = myhash.defineDone();
   count = 1;
  end;
 
 rc = myhash.ref();
 file log;
 if (_n_ = 12) then do;
     rc = iter.first();
     do while(rc = 0);
     rc = myhash.sum(sum: count);
         put key_id= count=;
         rc = iter.next();
     end;
 end;
run;

The following lines are written to the SAS log:

key_id=1 count=4
key_id=2 count=3
key_id=3 count=2
key_id=4 count=1
key_id=5 count=2

See Also

Last updated: August 19, 2026