/***************************************************************************************************************** SAS file name: locked_iterator.sas File location: __________________________________________________________________________________________________________________ Purpose: To explore how the SAS hash iterator locks item-groups in hash objects. And how to unlock them. Author: Peter Clemmensen Creation Date: 23/08/2020 This program supports the blog post "SAS Hash Object Locked by an Iterator Explained" on SASnrd.com *****************************************************************************************************************/ /* When does an iterator lock a hash object? */ /* The Clead method never works when an iterator resides in a hash object */ data _null_; dcl hash h(); h.definekey('k'); h.definedone(); dcl hiter hi('h'); do k = 1 to 3; h.add(); end; hi.first(); put k=; h.clear(); run; /* Only the item group that the iterator currently dwells within is locked */ data _null_; dcl hash h(ordered : "Y"); h.definekey('k'); h.definedone(); dcl hiter hi('h'); do k = 1 to 3; h.add(); end; hi.first(); put k=; h.remove(key : 2); /* No Error */ h.remove(key : 1); /* Error */ run; /* Replacement is not affected by the lock */ data _null_; dcl hash h(ordered : "Y"); h.definekey('k'); h.definedata('k', 'd'); h.definedone(); dcl hiter hi('h'); do k = 1 to 3; d = k * 2; h.add(); end; hi.first(); put (k d)(=); h.replace(key : 1, data : 10, data : 10); hi.first(); put (k d)(=); run; /* Removedup and Replacedup Method */ data have; input k d; datalines; 1 10 1 20 1 30 2 5 2 10 2 15 3 20 3 30 3 40 ; 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 : 1); h.find_next(); hi.first(); put (k d)(=); h.replacedup(data : k, data : 100); /* No Error */ h.removedup(); /* Error */ run;