/***************************************************************************************************************** SAS file name: PROC_FCMP_Hash.sas File location: __________________________________________________________________________________________________________________ Purpose: To demonstrate the use of the hash object in PROC FCMP Author: Peter Clemmensen Creation Date: 18/08/2019 This program supports the blog post "Use SAS Hash Object in PROC FCMP" on SASnrd.com *****************************************************************************************************************/ /* Example data */ data employees(drop=i); length empid $12; array first_names{20} $15 _temporary_ ("Paul", "Allan", "Thomas", "Michael", "Chris", "David", "John", "Jerry", "James", "Robert", "William", "Richard", "Bob", "Daniel", "Paul", "George", "Larry", "Eric", "Charles", "Stephen"); array last_names{20} $15 _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 1e6; first_name=first_names[ceil(rand("Uniform")*20)]; last_name=last_names[ceil(rand("Uniform")*20)]; empid=compress(uuidgen(), '-'); output; end; run; data ids(keep=empid); length empid $12; do p=1 to 5e5; set employees(keep=empid) point=p; output; end; do i=1 to 1e5; empid=compress(uuidgen(), '-'); output; end; stop; run; /* The usual approach */ data test; if _N_=1 then do; declare hash h(dataset:'ids'); h.definekey('empid'); h.definedone(); end; set employees; if h.check()=0; run; /* Write a function to create hash object and search for a string */ proc fcmp outlib=work.functions.fun; function hashcheck(empid $); declare hash h(dataset: 'ids'); rc=h.definekey('empid'); rc=h.definedone(); rc=h.check(); return (rc); endsub; run;quit; options cmplib=work.functions; data test2; set employees(where=(hashcheck(empid)=0)); run; /* Write a function to search for and look up a single value */ proc fcmp outlib=work.functions.fun; function findem(empid $) $; length first_name $ 100; declare hash h(dataset:'employees'); rc = h.definekey('empid'); rc = h.definedata('first_name'); rc = h.definedone(); call missing(first_name); rc = h.find(); return (first_name); endsub; run; /* Call Findem Function */ options cmplib=work.functions; data test3; set ids; length first_name $ 100; first_name=findem(empid); run; /* Write a SUBROUTINE to search for a key value and return multiple values */ proc fcmp outlib=work.functions.fun; subroutine findem2(empid $, first_name $, last_name $); outargs first_name, last_name; length first_name $ 100 last_name $ 100; declare hash h(dataset:'employees'); rc = h.definekey('empid'); rc = h.definedata('first_name', 'last_name'); rc = h.definedone(); call missing(first_name, last_name); rc = h.find(); endsub; run; /* Call Findem2 Subroutine */ options cmplib=work.functions; data test4; set ids; length first_name $ 100 last_name $ 100; call findem2(empid, first_name, last_name); run;