Attachment 2.1.3  Combining the S&P data with the IBES data

*  source file: ojoin.sas ;                                                      
*  Combining S&P data with IBES data;
                                                                                
%macro ojoin(out=ojoin, in1=, key1=, in2=, key2=, keylen=);                     
data &out(drop=astart bstart bkey oakey obkey                                   
           rename=(akey=&key1));                                                
  %if &keylen^=%str()                                                           
      %then %str(length akey bkey $ &keylen;);                                  
  aobs=1;      /*initialize pointer variables to beginning*/                    
  bobs=1;                                                                       
  link geta;   /*and get first obs from each*/                                  
  link getb;                                                                    
  do while ((aobs <= n_of_a) & (bobs <= n_of_b)); /*while more data*/           
    oakey = akey;     /*keep track of current key value in each set*/           
    obkey = bkey;                                                               
                                                                                
*--if the keys are unequal, advance the lower and try again;                    
                                                                                
    if (akey < bkey) then                                                       
      do;                                                                       
        astart = aobs;                                                          
        do aobs = astart to n_of_a until (akey > oakey);                        
          link geta;                                                            
        end;                                                                    
      end;                                                                      
    else if (akey > bkey) then                                                  
     do;                                                                        
        bstart = bobs;                                                          
        do bobs = bstart to n_of_b until (bkey > obkey);                        
          link getb;                                                            
        end;                                                                    
      end;                                                                      
    else       /*keys match*/                                                   
     do;                                                                        
       bstart = bobs;   /*start here in b often as needed for a*/               
       do until (akey ^= oakey);   /*until a key change in a*/                  
          do bobs = bstart to n_of_b;  /*for the matching key in b*/            
            link getb;                                                          
            if bkey ^= akey then goto bbreak;  /*no break!*/                    
            output;                                                             
          end;    /*of by group in b*/                                          
          bbreak:                                                               
          aobs = aobs + 1;                                                      
          if aobs > n_of_a then stop;  /*kludge, kludge!!*/                     
          link geta;                                                            
       end; /*of by group in a*/                                                
     end;  /*of code for matching by groups*/                                   
  end;  /*end of data*/                                                         
stop;                                                                           
                                                                                
  geta:                                                                         
    set &in1(rename=(&key1=akey)) point=aobs nobs=n_of_a;                       
return;                                                                         
  getb:                                                                         
    set &in2(rename=(&key2=bkey)) point=bobs nobs=n_of_b;                       
return;                                                                         
run;                                                                            
%mend ojoin;                                                                    

This page hosted by Get your own Free Home Page