/***************************************************************************************************************** SAS file name: Hash_Object_Group File location: _________________________________________________________________________________________________________________ Purpose: To demonstrate how to use the SAS hash object to group, but not sort, data values for by group processing with the NOTSORTED Option. Author: Peter Clemmensen Creation Date: 18/05/2018 This program supports the example page "Group Variable Values With Hash Object In SAS" on SASnrd.com *****************************************************************************************************************/ /* Example Data */ data MyData(drop=i); length CustomerID $6 Purchase 8; call streaminit(123); do i=1 to 20e6; ID=put(rand("integer", 1, 1000), z6.); Purchase=rand("integer", 100, 5000); output; end; run; /* Group data with hash object */ data _null_; if 0 then set MyData; if _N_=1 then do; declare hash h(dataset:"MyData", multidata:"Y"); h.definekey("CustomerID"); h.definedata(all:"Y"); h.definedone(); end; h.output(dataset:"GroupedData"); run; /* By group processing with the NOTSORTED Option */ data Accumulate; set GroupedData; by CustomerID notsorted; if first.CustomerID then sum=0; sum=sum+Purchase; if last.CustomerID; run; /* The usual approach: Sort and by group processing */ proc sort data=MyData; by CustomerID; run; data Accumulate; set MyData; by CustomerID; if first.CustomerID then sum=0; sum=sum+Purchase; if last.CustomerID; run;