/***************************************************************************************************************** SAS file name: min_max_hash.sas File location: __________________________________________________________________________________________________________________ Purpose: To demonstrate how to use an ordered hash object to find minimum and maximum values. Author: Peter Clemmensen Creation Date: 18/08/2020 This program supports the blog post "Find Min and Max Values with Hash Object in SAS - SASnrd" on SASnrd.com *****************************************************************************************************************/ /* Example data */ data have; input k d; datalines; 1 10 1 20 1 30 2 5 2 10 2 15 3 20 3 30 3 40 ; /* First approach - Sorted input */ data want; if _N_ = 1 then do; declare hash h (dataset : 'have(obs=0)', ordered : 'A', multidata : 'Y'); h.definekey ('d'); h.definedata(all : 'Y'); h.definedone(); declare hiter hi ('h'); end; do until (last.k); set have; by k; h.add(); end; _N_ = hi.next(); _N_ = hi.prev(); h.clear(); run; /* Second approach - Not sorted input - Hash of hashes */ data want; dcl hash HoH(ordered : 'A'); HoH.definekey('k'); HoH.definedata('h', 'hi', 'k'); HoH.definedone(); dcl hiter HoHiter('HoH'); do until (lr); set have end=lr; if HoH.find() ne 0 then do; dcl hash h(dataset : 'have(obs=0)', multidata : 'Y', ordered : 'A'); h.definekey('d'); h.definedata(all : 'Y'); h.definedone(); dcl hiter hi('h'); HoH.add(); end; h.add(); end; do while(HoHiter.next() = 0); _N_ = hi.next(); _N_ = hi.prev(); output; end; run;