/***************************************************************************************************************** SAS file name: hoh_applications.sas File location: __________________________________________________________________________________________________________________ Purpose: To demonstrate some preactical applications of the Hash of Hashes technique. Author: Peter Clemmensen Creation Date: 06/08/2020 This program supports the blog post "A Few Hash of Hashes Applications" on SASnrd.com *****************************************************************************************************************/ /* Example Data */ data have; input N1-N5; cards; 3 5 4 7 9 1 6 4 2 5 1 5 4 7 7 8 6 4 9 3 1 5 4 1 8 ; /* NLevels Option */ proc freq data=have nlevels; ods output nlevels=want_nlevels; run; /* HoH Nlevels */ data want_hoh (keep = nvar count); set have end = end; array nn N1-N5; if _n_ = 1 then do; dcl hash hh (); hh.definekey ("_i_"); hh.definedata ("h"); hh.definedone (); do over nn; dcl hash h (); h.definekey ("_n_"); h.definedone (); hh.add(); end; end; do over nn; hh.find(); h.ref (key:nn, data:nn); end; if end then do over nn; Nvar = put (vname (nn), $8.); hh.find(); Count = h.num_items; output; end; run; /* Single key changes in multi key hashes */ data _null_; dcl hash hoh (); hoh.definekey("k1"); hoh.definedata("h", "hi", "k1"); hoh.definedone(); dcl hiter i ("hoh"); dcl hash h; do until (lr); set have end=lr; if hoh.find() ne 0 then do; h = _new_ hash (multidata : "Y"); h.definekey ("k1", "k2"); h.definedata ("k1", "k2", "d"); h.definedone(); declare hiter hi ("h"); hoh.add(); end; h.add(); end; do while (i.next() = 0); put "** New key **"; do while (hi.next()=0); put (k1 k2 d)(=); end; end; run;