/***************************************************************************************************************** SAS file name: Hash_Vs_Index_Search File location: _________________________________________________________________________________________________________________ Purpose: To demonstrate the differences between the Hash Object and an Index as a lookup tool in SAS Author: Peter Clemmensen Creation Date: 30/04/2018 This program supports the example page "Comparing The Hash Object And SAS Index" on SASnrd.com *****************************************************************************************************************/ data MasterHashVsIndex; length ID $6 first_name $20 last_name $20 method $20 elapsedtime 8 nobs 8; run; data callstack; length string $5000; do obs=20e5 to 10e7 by 20e5; string=compbl(cats( " data MyData(drop=i); length ID $6 first_name $20 last_name $20; array first_names{20}$20 _temporary_ ('Paul', 'Allan', 'Bob', 'Michael', 'Chris', 'David', 'John', 'Jerry', 'James', 'Robert', 'Karen', 'Betty', 'Helen', 'Sandra', 'Sharon', 'Laura', 'Michelle', 'Angela', 'Melissa', 'Amanda'); array last_names{20}$20 _temporary_ ('Smith', 'Johnson', 'Williams', 'Jones', 'Brown', 'Miller', 'Wilson', 'Moore', 'Taylor', 'Hall', 'Anderson', 'Jackson', 'White', 'Harris', 'Martin', 'Thompson', 'Robinson', 'Lewis', 'Walker', 'Allen'); call streaminit(123); do i=1 to (", obs,"); ID=uuidgen(); first_name=first_names[ceil(rand('Uniform')*20)]; last_name=last_names[ceil(rand('Uniform')*20)]; output; end; run; proc datasets library=work nolist; modify MyData; index delete _all_; index create ID / nomiss; run;quit; data HashvsIndex", obs, "(drop=rc i start); length ID $6 first_name $20 last_name $20 method $20; declare hash h(dataset:'MyData', hashexp:0); h.definekey('ID'); h.definedata(all:'Y'); h.definedone(); method='Hash'; nobs=", obs, "; start=time(); do i=1 to 10e3; ID=uuidgen(); rc=h.find(key:ID); end; elapsedtime=time()-start; put +10 '############################## ' elapsedtime ' ##############################'; output; method='Index'; nobs=", obs, "; start=time(); do i=1 to 10e3; ID=uuidgen(); set MyData key=ID; _ERROR_=0; end; elapsedtime=time()-start; put +10 '############################## ' elapsedtime ' ##############################'; output; stop; run; proc append base=MasterHashVsIndex data=HashvsIndex", obs, "; run;quit; proc datasets lib=work nolist; delete HashvsIndex", obs, "; run;quit; " )); output; call execute(string); end; run; title "Elapsed search time for increasing observations to search"; proc sgplot data=MasterHashVsIndex; where elapsedtime ne .; series x=nobs y=elapsedtime / markers group=method markerattrs=(symbol=circlefilled); xaxis label="Observations to Search"; yaxis label="Seconds"; keylegend / position=nw location=inside across=1 noborder title=""; format nobs e10.; run; title;