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
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)
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.