/***************************************************************************************************************** SAS file name: Replacedup_Method.sas File location: __________________________________________________________________________________________________________________ Purpose: To demonstrate the use of the Replacedup() hash object method Author: Peter Clemmensen Creation Date: 13/08/2020 This program supports the blog post "Explore the SAS Hash Object Replacedup 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.replacedup(data : k, data : 100); do while (hi.next()=0); put (k d)(=); end; run; /* Selectively updating 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, 30) then rc2 = h.replacedup(data : k, data : 100); rc = h.find_next(); end; end; do while (hi.next()=0); put (k d)(=); end; run; /* The Do_Over Method */ 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.replacedup(data : k, data : 100); end; end; do while (hi.next()=0); put (k d)(=); end; run;