Previous Page | Next Page

Hash and Hash Iterator Object Language Elements

REF Method



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

Syntax
Arguments
Details
Examples
See Also

Syntax

rc=object.REF(<KEY: keyvalue-1,..., KEY: keyvalue-n, DATA: datavalue-1,
..., DATA: datavalue-n>);


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.  [cautionend]

For more information about key summaries, see SAS Language Reference: Concepts.


Examples

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;

The following lines are written to the SAS log.

Output of DATA Using the REF Method

  
        Obs    count    key
          1       4       1
          2       3       2
          3       2       3
          4       1       4
          5       2       5

See Also

Methods:

ADD Method

FIND Method

CHECK Method

Previous Page | Next Page | Top of Page