BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PGStats
Opal | Level 21

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
1 ACCEPTED SOLUTION

Accepted Solutions
FriedEgg
SAS Employee

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

5 REPLIES 5
FriedEgg
SAS Employee

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.

PGStats
Opal | Level 21

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

PG
FriedEgg
SAS Employee

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

Haikuo
Onyx | Level 15

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

FriedEgg
SAS Employee

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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