Using hash objects with duplicates

Reply
N/A
Posts: 1

Using hash objects with duplicates

How do I access duplicates in a hash object because the .find() returns the first unique observation of a key.?

Example:

data applications;

    

length id $13;

input id $ application_date : date9. amount;

datalines;

7902065874058 12FEB2013 15894

7902065874058 25MAY2013 25484

9202065870086 09MAR2013 2300

9202065870086 01MAY2013 5685

9012125412508 03MAY2013 2300

;

run;

Contributor
Posts: 25

Re: Using hash objects with duplicates

I am not sure about what duplicates you are referring to.

But I assume that the ID is duplicate for specific month?

You can use the hither object to navigate through a hash object.

Think of the hither object when you browsing the internet looking for a specific item in a data list. You will find that there is a first next last prev buttons. The hither object works in the same way.

See the code below how the hither object is in action.

data applications_date_range(drop=i);

  format application_date date9.;

  if 0 then set applications(keep=id application_date amount);

  if _N_=1 then do;

  declare hash Applications(dataset:'applications');

  Applications.definekey('id','application_date');

    Applications.definedata('id','application_date', 'amount');

  Applications.definedone();

  declare hiter a('Applications');

  end;

  a.first();

  do i = 1 to 4;

  if  application_date >= '01MAY2013'D

  and application_date <= '31MAY2013'D then output;

  a.next();

  end;

  stop;

run;

proc print data=applications_date_range; run;

Respected Advisor
Posts: 4,173

Re: Using hash objects with duplicates

Since SAS 9.2 a hash object can have duplicate keys and you don't need a hash iterator object to loop over the duplicates.

Have a read of this document http://support.sas.com/rnd/base/datastep/dot/better-hashing-sas92.pdf  It also gives you an example of how to loop over the duplicate keys.

Ask a Question
Discussion stats
  • 2 replies
  • 168 views
  • 1 like
  • 3 in conversation