No problem. I haven't done much with hash but took a shot at my understanding of what you want (no worrying about optimization). Could be mistakes, and would want to add error handling. But figured I'd share in case its of interest. data members; input member_id member_zip; cards; 1 02138 2 02138 3 02139 4 02139 5 02860 6 02806 ; data providers; input pcp_id provider_zip capacity; cards; 15 02139 1 18 02139 2 21 02806 20 ; run; data want(keep=member_id member_zip pcp_id provider_zip distance); if 0 then set providers; set members end=last; if _N_ = 1 then do; declare hash h(dataset:'providers', duplicate:"error"); h.defineKey('pcp_id'); h.defineData(ALL:"YES"); h.defineDone(); declare hiter hiter ("h" ) ; end; rc=hiter.first(); do while(rc=0); if capacity>0 then do; distance=zipcitydistance(member_zip,provider_zip); if .Z<distance<mindistance or missing(mindistance) then do; MinDistance=Distance; SelectedPCP_ID=pcp_ID; end; end; rc=hiter.next(); end; *now we have iterated over all providers, and selected closest one; *need to decrement the capacity in the hash table; PCP_ID=SelectedPCP_ID; h.find(); capacity=capacity-1; h.replace(); *output dataset of how much capacity remains at each provider; if last then h.output (dataset: "Providers2") ; run;
... View more