Return to Snippet

Revision: 46932
at May 27, 2011 19:45 by rambles


Initial Code
**  Dummy code; 
data input ;                                                                                    
   do k1 = 1e6 to 1 by -1 ;                                                                     
      k2 = put (k1, z7.) ;                                                                      
      do num = 1 to ceil (ranuni(1) * 6) ;                                                      
         output ;                                                                               
      end ;                                                                                     
   end ;                                                                                        
run ;                                                                                           

**  Standard approach using Proc Summary;  
proc summary data = input nway ;                                                                
   class k1 k2 ;                                                                                
   var num ;                                                                                    
   output out = summ_sum (drop = _:) sum = sum ;                                                
run ;                                          
                                                                                               
**  Alternative using the hash object;
data _null_ ;                                                                                   
   if 0 then set input ;                                                                        
                                 
   dcl hash hh (hashexp:16) ;                                                                   
   hh.definekey  ('k1', 'k2'       ) ;                                                          
   hh.definedata ('k1', 'k2', 'sum') ;                                                          
   hh.definedone () ;                                                                           
   do until (eof) ;                                                                             
      set input end = eof ;                                                                     
      if hh.find () ne 0 then sum = 0 ;                                                         
      sum ++ num ;                                                                              
      hh.replace () ;                                                                           
   end ;
   rc = hh.output (dataset: 'hash_sum') ;                                                       
run ;

Initial URL
http://www2.sas.com/proceedings/sugi30/236-30.pdf

Initial Description
This snippet comes directly from Paul M. Dorfman's paper on programming with Hash objects.  The hash object is useful when summarising huge datasets that aren't sorted and indexed by the variable(s) to be summarised; they can often be quicker than proc summary and are certainly less machine intensive.

Initial Title
Summary datasets using hashes

Initial Tags


Initial Language
SAS