/***************************************************************************************************************** SAS file name: hash_object_dynamic File location: _________________________________________________________________________________________________________________ Purpose: To demonstrate how to use the SAS hash object as a dynamic structure. Author: Peter Clemmensen Creation Date: 28/12/2018 This program supports the example page "The SAS Hash Object as a Dynamic Placeholder" on SASnrd.com *****************************************************************************************************************/ /* Example data */ data Example(drop=i); do i=1 to 100; date=today()-i*rand('integer', 1, 100); ID=put(rand('integer', 1, 10), z4.); var1=rand('integer', 1, 100); var2=rand('integer', 1, 100); output; end; format date date9.; run; /* Flag 5th occurence of a variable value */ data test1(drop=rc); if _N_ = 1 then do; declare hash h(); h.defineKey('ID'); h.defineData('c'); h.defineDone(); end; set Example; flag=0; rc=h.check(); if rc ne 0 then do; c=1; h.add(); end; if rc=0 then do; rc=h.find(); c=c+1; if c=2 then flag=1; rc=h.replace(); end; run; /* Output observations the sum of var1 exceeds 500 */ data _null_; if _N_ = 1 then do; declare hash h(); h.defineKey('ID'); h.defineData('sum_var1'); h.defineDone(); declare hash ids(); ids.defineKey('ID'); ids.defineData('var1', 'var2', 'sum_var1'); ids.defineDone(); end; set Example end=lr; rc=h.check(); if rc ne 0 then do; sum_var1=var1; h.add(); end; if rc=0 then do; rc=h.find(); sum_var1=sum_var1+var1; if sum_var1 ge 500 and ids.check() ne 0 then ids.add(); rc=h.replace(); end; if lr then ids.output(dataset:'IdsOver500'); run;