DATA Step, Macro, Functions and more

Iterate the hash more than one time

New Contributor
Posts: 4

Iterate the hash more than one time

Hi all,


I am using hash iteration in below code for matching the observations of two groups, how can I iterate the hash for N times?

(the below code matches one control to one treated group, how can i get 5 controls for each treated?

( This is a without replacemnet matching methd)

data want;
 length psC 8;
 length idC 8;
if _N_= 1 then do;
 declare hash h(dataset: "C", ordered: 'no');
declare hiter iter('h');
h.defineData('psC', 'idC');
 call missing(idC, pscoreC);
 set T;
 retain BestDistance 99;
 rc= iter.first();
 if (rc=0) then BestDistance= 99;
do while (rc= 0);
 ScoreDistance= abs(pscoreT - pscoreC);
if ScoreDistance < BestDistance then do;
BestDistance= ScoreDistance;
 IdSelectedControl= idC;
MatchedToTreatID= idT;
 if (rc~= 0) then do;
rc1= h.remove(key: IdSelectedControl);



your help will be appreciated


Respected Advisor
Posts: 4,695

Re: Iterate the hash more than one time


Using your existing code as a starting point the a bit wasteful but least code change would be to simply add another loop around all your lookup logic.

  do i=1 to 5;
    rc= iter.first();
          rc1= h.remove(key: IdSelectedControl);

You are aware that by always removing a record from the hash table you won't end up with the possible best distance for all your controls. You just get the best distance for what remains in the hash. If that's not what you're after then you would first have to calculate all the distances and only then filter the records ensuring that you pick a control only once (but then the one with the best distance for all your source records).


As for your current logic: A bit a less wasteful approach could be to first collect all the results in a second hash (ordered by ascending best distance values) and once iterated fully over the first hash output the first 5 records in the 2nd hash (and also delete the matching records in the first hash at the same time)

Posts: 2,319

Re: Iterate the hash more than one time

1. Supplying working code would be nice. pscoreC and psC are the same I suppose?

2. Formatting the code would be nice too. Use the running man or the {i} icon.

3. Supplying sample data to run the code on, together with expected results would be very useful too.

Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation