DATA Step, Macro, Functions and more

Hash Question

Reply
Occasional Contributor
Posts: 18

Hash Question

The following script selects the first payment that meets the criteria of the given promise record by using a hash table.  I need to modify this to render ALL records in the payment table that meet the promise criteria.

 

So for the first record in promise (acctnum = 100 dated 3/15) - i would see two payments (dated 3/21 and 3/25) for that promise record in the output. no records would be output for the second observation (acctnum = 100 dated 3/21).

 

Can the below script modified to accomplish this?

 

 

 

data Promise ;
input acctnum  promisedate : mmddyy12. promiseamt (bwindow  ewindow ) (: mmddyy12.);
format promisedate bwindow ewindow mmddyy10.;
cards;
100  3/15/2015 100  3/15/2015 3/28/2015
100  3/21/2015 150  3/21/2015 4/1/2015
200  3/15/2015 100  3/15/2015 3/28/2015
200  3/21/2015 150  3/21/2015 4/1/2015
;
run;

 

data Payments ;
input acctnum  paydate  : mmddyy12. payamt ;
format       paydate   mmddyy10.;
cards;
100  3/21/2015 100
100  3/25/2015 150
100  5/25/2015 150
200  3/21/2015 100
200  3/25/2015 150
200  3/28/2015 150
200  5/25/2015 150
;
run;

data want;
 if _n_ eq 1 then do;
  if 0 then set Promise(keep=acctnum  bwindow ewindow);
  declare hash h(dataset:'Promise(keep=acctnum  bwindow ewindow)',multidata:'y');
  h.definekey('acctnum');
  h.definedata('bwindow','ewindow');
  h.definedone();
 end;
set  Payments;
rc=h.find();
do while(rc=0);
 if bwindow le paydate le ewindow then do;
                                          found=1;
                                                  ​  h.removedup();
                                          leave;
                                       end;
 rc=h.find_next();
end;
drop rc;
run;

Valued Guide
Posts: 858

Re: Hash Question

I am missing something, can you explain why the second record does not fall into the criteria? 

Occasional Contributor
Posts: 18

Re: Hash Question

Once a payment record/observation has been used..  i dont want to use it again for any further observation in the promise dataset. 

 

The first two obs of the payment dataset would go witht the first observation in the promise dataset. The second observation in the promise dataset would get no matches. 

Valued Guide
Posts: 858

Re: Hash Question

I was hoping someone else answered so I can see the solution.  I'm not the best person to answer this question but see if this gets the desired results, with the test data it works:

 

data want;
length acctnum 8. promisedate 8. promiseamt 8. bwindow 8. ewindow 8.;
if _N_ = 1 then do;
    declare hash e(dataset:'promise');
    e.definekey('acctnum');
    e.definedata('promisedate','promiseamt','bwindow','ewindow');
    e.definedone();
end;
set Payments;
drop rc;
rc = e.find();
if rc = 0 and bwindow <= paydate <= ewindow;
format promisedate bwindow ewindow mmddyy10.;
run;

Occasional Contributor
Posts: 18

Re: Hash Question

Thanks for your help.

 

I will give this a try and see how it works.

Ask a Question
Discussion stats
  • 4 replies
  • 192 views
  • 0 likes
  • 2 in conversation