DATA Step, Macro, Functions and more

Hash.ref() behaviour

Accepted Solution Solved
Reply
Respected Advisor
Posts: 4,648
Accepted Solution

Hash.ref() behaviour

Hi Hash experts, I'm trying to teach myself to use the hash and hiter component objects (v9.3) and there is something I don't understand. It is about the ref() method of the hash object. It says in the doc:

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.

That would be very usefull indeed. But the following little test seems to contradict that statement.

data _null_;

length k $15;

length d $15;

/* Declare and instantiate hash object "myhash" */

declare hash myhash;

myhash = _new_ hash( );

/* Define key and data variables */

rc = myhash.defineKey('k');

rc = myhash.defineData('d');

rc = myhash.defineDone( );

/* avoid uninitialized variable notes */

call missing(k, d);

/* Create constant key and data values */

rc = myhash.add(key: 'Labrador', data: 'Retriever');

/* Give initial value to data variable */

d = 'TEST';

/* Call ref method with existing key */

rc = myhash.ref(key: 'Labrador', data: 'Golden');

/* Check data variable */

put d;

stop;

run;

TEST

I was expecting Retriever.

Can somebody explain?

PG

PG

Accepted Solutions
Solution
‎05-23-2012 03:58 PM
Trusted Advisor
Posts: 1,300

Re: Hash.ref() behaviour

SAS has recognized the issue in this usage note:

http://support.sas.com/kb/45/093.html

The REF method is a consolidation of the CHECK and ADD methods.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,300

Re: Hash.ref() behaviour

I have never found this note to actually hold true.  The ref method is supposed to be a combination of the 'find' and 'add' methods, which would do what you are seeking when used separately.  This seems to hold true in both 9.2 and 9.3 and I have never seen it replace the data value in PDV when a key is found when using the ref method.

data _null_;

dcl hash m();

  m.definekey('k');

  m.definedata('d');

  m.definedone();

k='Labrador'; d='Retriever';

m.add();

d='TEST';

rc=m.ref();

put _all_;

/* k=Labrador d=TEST rc=0 _ERROR_=0 _N_=1 */

rc=m.ref(key:'Labrador',data:'blah');

put _all_;

/* k=Labrador d=TEST rc=0 _ERROR_=0 _N_=1 */

/* how the ref method is expected to behave */

rc=m.find();

  if rc^=0 then rc=m.add();

put _all_;

/* k=Labrador d=Retriever rc=0 _ERROR_=0 _N_=1 */

stop;

run;

The ref method, to me, seems to be more accurately defined as a combination of the check and add methods rather than the find and add methods.

Respected Advisor
Posts: 4,648

Re: Hash.ref() behaviour

Thank you FriedEgg. Do you know of other inaccuracies in the component objects documentation? - PG

PG
Trusted Advisor
Posts: 1,300

Re: Hash.ref() behaviour

I am not familiar with any other issues like this as far as hash and hash iterator methods are concerned.

Respected Advisor
Posts: 3,124

Re: Hash.ref() behaviour

I concur. The help doc for ref() is erroneous at least for SAS 9.2.

Where it says:

"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();

"

While like FriedEgg said, it should be:

rc=h.check();

if rc ne 0 then rc=h.add();

else do nothing.

    It also occurs to me rc=ref() has always been '0'.

Haikuo

Solution
‎05-23-2012 03:58 PM
Trusted Advisor
Posts: 1,300

Re: Hash.ref() behaviour

SAS has recognized the issue in this usage note:

http://support.sas.com/kb/45/093.html

The REF method is a consolidation of the CHECK and ADD methods.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 271 views
  • 6 likes
  • 3 in conversation