/***************************************************************************************************************** SAS file name: Removedup_Method.sas File location: __________________________________________________________________________________________________________________ Purpose: To demonstrate the use of the Removedup() hash object method Author: Peter Clemmensen Creation Date: 11/08/2020 This program supports the blog post "Explore the SAS Hash Object Removedup Method" 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 ; /* A simple example */ data _null_; declare hash h(dataset : "have", multidata : "Y", ordered : "A"); h.definekey("k"); h.definedata("k", "d"); h.definedone(); declare hiter hi("h"); k = .; d = .; h.find(key : 2); h.find_next(); h.removedup(); do while (hi.next()=0); put (k d)(=); end; run; /* Selective deletion of multiple items */ data _null_; declare hash h(dataset : "have", multidata : "Y", ordered : "A"); h.definekey("k"); h.definedata("k", "d"); h.definedone(); declare hiter hi("h"); k = .; d = .; do k = 1, 3; do rc = h.find() by 0 while (rc = 0); if d in (20) then rc2 = h.removedup(); *if d in (20, 30) then rc2 = h.removedup(); rc = h.find_next(); end; end; do while (hi.next()=0); put (k d)(=); end; run; /* The Do_Over Method is simpler */ data _null_; declare hash h(dataset : "have", multidata : "Y", ordered : "A"); h.definekey("k"); h.definedata("k", "d"); h.definedone(); declare hiter hi("h"); k = .; d = .; do k = 1, 3; do while (h.do_over()=0); if d in (20, 30) then rc2 = h.removedup(); end; end; do while (hi.next()=0); put (k d)(=); end; run; /* Simplifying a documentation example */ data testdup; length key_id value 8; input key_id value; datalines; 1 10 2 11 1 15 3 20 2 16 2 9 3 100 5 5 1 5 4 6 5 99 ; data _null_; length r 8; dcl hash h(dataset:'testdup', multidata: 'y', ordered: 'y'); h.definekey('key_id'); h.definedata('key_id', 'value'); h.definedone(); call missing (key_id, value); do key_id = 1 to 5; rc = h.find(); if (rc = 0) then do; h.has_next(result: r); if (r ne 0) then do; h.find_next(); h.removedup(); end; end; end; dcl hiter i('h'); rc = i.first(); do while (rc = 0); put key_id= value=; rc = i.next(); end; run; /* Do_Over method instead - Much simpler */ data _null_; length r 8; dcl hash h(dataset:'testdup', multidata: 'y', ordered: 'y'); h.definekey('key_id'); h.definedata('key_id', 'value'); h.definedone(); dcl hiter i('h'); call missing (key_id, value); do key_id = 1 to 5; do c = 1 by 1 while (h.do_over()=0); if c = 2 then h.removedup(); end; end; do while (i.next()=0); put key_id= value=; end; run;