BookmarkSubscribeRSS Feed
Rammed
Calcite | Level 5

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.defineKey('idC');
h.defineData('psC', 'idC');
h.defineDone();
 call missing(idC, pscoreC);
end;
 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;
end;
rc= iter.next();
 if (rc~= 0) then do;
output;
rc1= h.remove(key: IdSelectedControl);
end;

end;
run;

 

your help will be appreciated

Rama

2 REPLIES 2
Patrick
Opal | Level 21

@Rammed

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();
      ....
          output;
          rc1= h.remove(key: IdSelectedControl);
        end;
    end;
  end;

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)

ChrisNZ
Tourmaline | Level 20

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 653 views
  • 2 likes
  • 3 in conversation