BookmarkSubscribeRSS Feed
sahaji
Calcite | Level 5

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.

4 REPLIES 4
CTorres
Quartz | Level 8

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

Regards,

sahaji
Calcite | Level 5

Hi CTorres,

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

CTorres
Quartz | Level 8

be sure to use the colon(:) in the Multidata option: Multidata:

Maybe double quotes?

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

Regards

Patrick
Opal | Level 21

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=_:);

  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;

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
  • 4 replies
  • 945 views
  • 0 likes
  • 3 in conversation