DATA Step, Macro, Functions and more

I Need clarification on Hash Object

Reply
Occasional Contributor
Posts: 15

I Need clarification on Hash Object

Hi,

I am learning hash objects ,when I was working on the below scenario,it is giving as specified below.please clarify.

I have two data sets ,I am assuming that one is bigger one and one is smaller one.

the smaller data(data1) set consists of following data.

connection_id credit_teamid

the bigger one(master) data set consists of following data.

Connection_id          credit_teamid      unit_desc                     group_code

DATA Hash_sample(drop=search);

length  connection_id 10;

length  credit_team_id unit_desc group_code $20;

if _n_=1 then do;

declare hash first_HT(dataset:"master");

first_ht.definekey("connection_id","credit_team_id");

first_ht.definedata("unit_desc","group_code");

first_ht.definedone();

end;

set data1;

search=first_ht.find();

if search=0 then output;

run;

Here I'm getting the output like this


Even though master data set consists of same connection id and credit team id's more than once,small data set is not merging with all the connection and credit team id's in the master data set.

It is only merging with first occurance(first.) of the connection id and credit team id's of the master data set.

Please clarify.

Regular Contributor
Posts: 180

Re: I Need clarification on Hash Object

Try using the Multidata: 'Y' option in the Declare Hash statement:  declare hash first_HT(dataset:"master", Multidata: 'Y');

Regards,

Occasional Contributor
Posts: 15

Re: I Need clarification on Hash Object

Hi CTorres,

I tried with the multidata,It is giving the error message :unknown argument Multidata.

Regular Contributor
Posts: 180

Re: I Need clarification on Hash Object

be sure to use the colon(Smiley Happy in the Multidata option: Multidata:

Maybe double quotes?

Look at the documentation: SAS(R) 9.3 Component Objects: Reference

Regards

Respected Advisor
Posts: 3,892

Re: I Need clarification on Hash Object

For loading "master" into a hash you would need "multidata" with the keys you've chosen. You then would also need to iterate through the groups with the same key as shown here:

SAS(R) 9.3 Component Objects: Reference

But actually for what I believe you're trying to achieve it would be much better to load the small data set into a hash. As the key variables exist in both "master" and "data1" you don't need to define the variables for the hash if "setting" master before the hash definition.

Code as below should do (not tested):

DATA Hash_sample (drop=_Smiley Happy;

  set master;

  if _n_=1 then

    do;

      declare hash first_HT(dataset:"data1");

      _rc=first_ht.definekey("connection_id","credit_team_id");

      _rc=first_ht.definedone();

    end;

  if first_ht.find()=0 then output;

run;

Ask a Question
Discussion stats
  • 4 replies
  • 242 views
  • 0 likes
  • 3 in conversation