DATA Step, Macro, Functions and more

Using hash tables

Reply
PROC Star
Posts: 1,836

Re: Using hash tables

Posted in reply to astha8882000

basically, if your hash obj doesn;t have any records and obj.find method wouldn't find the matching key in the hash object. This will result in a non zero return code. For example:

data xyz;

if _n_=1 then do;
if 0 then set sashelp.class;

dcl hash hh (dataset: 'sashelp.class(obs=0)');/*hash object with zero records*/

hh.definekey ('name');

hh.definedone();

end;
set sashelp.class;
rc=hh.find();/*return code non zero*/
run;
Contributor
Posts: 44

Re: Using hash tables

[ Edited ]
Posted in reply to novinosrin

So this is the structure of the overall code:

data xyz;
if _n_=1 then do;
dcl hash hh (dataset: 'abc');
hh.definekey ('phoneno');
hh.definedone();
end;
bunch of keep statements that are not part of abc or xyz
%INCLUDE INCLF(f1.sas);
if hh.find()=0;
%INCLUDE INCLF(f2.sas);
Bunch of if statements
run;

I think right now my main aim is to get rid of hash altogether since understanding is a little hard for me based on my lack of experience

PROC Star
Posts: 1,836

Re: Using hash tables

Posted in reply to astha8882000

Ok,  Can you create a new thread with a sample data and your required output so that the community can give you an alternative solution other than hashes? We don't even have to worry about hashes at all. You can get a completely new and simple script. Please make sure you get the requirements correct

Contributor
Posts: 44

Re: Using hash tables

Posted in reply to novinosrin
PROC Star
Posts: 1,836

Re: Using hash tables

Posted in reply to astha8882000

@astha8882000 just noticed. But I can't see your expected output sample for your input data sample, meaning you what to accomplish like data have(i see there is this bit) , please provide data want(for your sample). Thank you!

Contributor
Posts: 44

Re: Using hash tables

Posted in reply to novinosrin

The problem with that is, because of all the errors with my hash table code, I'm unable to run it and see what the solution might look like. Hence I wanted to get the hash table replaced so I can check and see what the result might be.

Super User
Posts: 10,280

Re: Using hash tables

Posted in reply to astha8882000

@astha8882000 wrote:

The problem with that is, because of all the errors with my hash table code, I'm unable to run it and see what the solution might look like. Hence I wanted to get the hash table replaced so I can check and see what the result might be.


Which means it is time to go back to square one.

  • what data do you have?
  • what do you want to achieve?

Once these questions are answered, you can start designing your solution.

Code that you do not understand is per definition crap and has to be discarded for unmaintainability. It might be that it's not crap for someone else (eg the original creator), but since it's obviously not sufficiently documented for you, can it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 44

Hash Tables

Posted in reply to astha8882000

I have never used hash before, saw this piece of code being included in many places within a big project, can someone please help explain what each line means?

 

IF _n_ = 1 THEN DO;
Dcl Hash hh (dataset: 'work.CCEM');
hh.DefineKey ('tel);
hh.DefineDone ();
End;

 

Super User
Posts: 10,280

Re: Hash Tables

Posted in reply to astha8882000

It declares a hash object with one variable that acts as key. The object is populated from column tel in dataset work.ccem.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
PROC Star
Posts: 1,836

Re: Hash Tables

Posted in reply to astha8882000

1. instantiate a hash object as soon as datastep loops past the data statement (_n_=1)

2. load the hash object with data by reading dataset work.CCEM

3. complete the instantiation with definedone

 

Contributor
Posts: 44

Re: Hash Tables

Posted in reply to novinosrin
Thank you.
Contributor
Posts: 44

Re: Hash Tables

Posted in reply to novinosrin

If I could add a follow up to this:

 

I have a dataset created 'abc' in a sasgrid location

I'm creating another dataset 'xyz' through that using hash table, for eg.,

 

data xyz;

if _n_=1 then do;

dcl hash hh (dataset: 'abc');

hh.definekey ('phoneno');

hh.definedone();

end;

followed by a bunch of keep statements

run;

 

when I try and run this, I get the following 2 errors:

ERROR: Undeclared key symbol phoneno for hash object
ERROR: DATA STEP Component Object failure. Aborted during the EXECUTION phase.

 

what does this even mean? what do I need to change to sort this? sorry I'm very new to hash tables

 

Super User
Posts: 10,280

Re: Hash Tables

Posted in reply to astha8882000

You need to set phoneno to a value so that the find() method works:

data abc;
input phoneno $;
cards;
1234567
;
run;

data xyz;
if _n_=1 then do;
  dcl hash hh (dataset: 'abc');
  hh.definekey ('phoneno');
  hh.definedone();
end;
*phoneno = '1234567';
found = hh.find();
run;

Run that code, and you'll get your ERROR. Remove the comment asterisk, and it'll be OK.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 10,280

Re: Hash Tables

Posted in reply to astha8882000

PS if you do this:

data xyz;
length phoneno $8;
if _n_=1 then do;
  dcl hash hh (dataset: 'abc');
  hh.definekey ('phoneno');
  hh.definedone();
end;
found = hh.find();
run;

there's no ERROR, but you won't find anything, as the empty string is not present as a key in the hash.

If your reference variable is present in another dataset, it will also work:

data def;
input phoneno $;
cards;
1234567
9865443
;
run;

data xyz;
set def;
if _n_=1 then do;
  dcl hash hh (dataset: 'abc');
  hh.definekey ('phoneno');
  hh.definedone();
end;
found = hh.find();
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Highlighted
Trusted Advisor
Posts: 1,256

Re: Hash Tables

Posted in reply to astha8882000

Just to add: Hash object hh will contain both a data item and a key item corresponding to variable tel, because the DefineData method is not used. (Or better: "would contain", if the closing single quote after "tel" wasn't missing.)

 

If you are an experienced data step programmer, I recommend that you familiarize yourself with the hash object. It's a really useful tool.

Ask a Question
Discussion stats
  • 31 replies
  • 139 views
  • 4 likes
  • 6 in conversation