/**********************************************************************/


filename tradein  '\ob generic-sequential-filename\obe ';
libname  tradelib '\ob generic-directory-name\obe ';     
                                                         
data tradelib.pfb;                                       
   infile tradein;                                       
   format tdate YYMMDD6.;                                
   input  @1  tdate   YYMMDD6.                           
          @7  carry   $char1.                            
          @8  broker  $char1.                               
          @9  matcher $char1.                               
          @10 buysell $char4.                               
          @14 qty                                           
          @20 descr   $char14.                              
          @35 price                                         
          @42 prin                                          
          @52 netprice;                                     
                                                            
      /* Compute commission from principal and net amount */
   comm=netprice-prin;                                      
   if comm<0 then comm=-comm;                               
run;                                                        


/**********************************************************************/


proc sort data=tradelib.pfb;             
   by broker descr matcher tdate buysell;


/**********************************************************************/


data tradelib.match;                                          
   set tradelib.pfb;                                          
   by broker descr matcher;                                   
   retain netgain  0    /* net profit or loss             */  
                        /* on the trade                   */  

          grandnet 0    /* grand total net profit         */  
                        /* or loss                        */  
                                                              
          brokenet 0    /* net profit or loss at broker   */  
          totcomm  0    /* total brokerage commissions    */  
                        /* paid                           */  
                                                              
          tvol     0    /* total volume for security      */  
          grandcom 0    /* grand total commissions paid   */

          buyprice 0    /* purchase price for the         */
                        /* security                       */
                                                           
          sellpric 0;   /* sale price for the security    */
                                                           
      /* BUYDATE and SELLDATE values are zeroed out each  */
      /* time a new description is seen. The first date   */
      /* on which a buy is encountered and the last date  */
      /* on which a sell is encountered are saved for     */
      /* the output report.                               */
   retain buydate selldate;                                 
                                                            
      /* Accumulate the net profit or loss for all        */
      /* trades at each brokerage house in the variable   */
      /* BROKENET.                                        */
   if first.broker then                                     
      brokenet=0;                                           
                                                           
      /* Reset accumulators to zero for                   */
      /* each new security seen.                          */
   if first.matcher then do;                                
      netgain=0;                                            
      totcomm=0;                                            
      tvol=0;                                               
      buydate=0;                                            
      selldate=0;                                           
      buyprice=0;                                           
      sellpric=0;                                           
      end; 


/**********************************************************************/


   if upcase(buysell)='BUY' then do;                        
                                                            
         /* Add trade quantity to total volume for        */
         /* the security.                                 */
      tvol=tvol+qty;                                        

         /* Subtract buys from net.                       */
      netgain=netgain-netprice;                             
                                                            
         /* Sum total paid for all purchases in buyprice. */
      buyprice=buyprice+netprice;                           
                                                            
         /* If the first purchase of the security,        */
         /* remember the date.                            */
      if buydate=0                                          
         then buydate=tdate;                                
      end; 


/**********************************************************************/


   else if upcase(buysell)='SELL' then do;                   
                                                             
         /* Add proceeds to net profit.                   */ 
      netgain=netgain+netprice;                              
                                                             
         /* Remember latest sale date.                    */ 
      selldate=tdate;                                        
                                                             
         /* Add this sale to total.                       */ 
      sellpric=sellpric+netprice;                            
                                                             
         /*  Show negative volume on short sale.          */ 
         /*  No purchases seen because they would         */ 
         /*  have come first in the sort order.           */ 
      if tvol=0                                              
      and last.descr                                         
      then tvol=tvol-qty;                                    
      end;                                                   
                                                             
      /* Accumulate the commissions paid on every trade.  */ 
   totcomm=totcomm+comm;


/**********************************************************************/


   if last.descr then do;                                   
                                                            
         /* If the position is not carried forward,       */
         /* it is closed. Accumulate net profit and       */
         /* commission by broker.                         */
      if carry=' ' then do;                                 
         grandnet=grandnet+netgain;                         
         brokenet=brokenet+netgain;                         
         grandcom=grandcom+totcomm;                         
         end;                                               

         /* The output data set gets a single             */
         /* observation for every security traded at      */
         /* brokerage house.                              */
      output;                                               
      end;                                                  
                                                            
      /* For each broker, report the net profit or loss   */
      /* for the year. These figures do not need to be    */
      /* in the output report (the IRS does not care      */
      /* what broker you use) so they are just written    */
      /* to the SAS log.                                  */
   if last.broker then do;                                  
      put 'broker net=' broker brokenet;                    
      end;


/**********************************************************************/


broker net=M 341.16                                         
broker net=W 310                                            


/**********************************************************************/

                                                            
      /* These data set variables are no longer needed.   */
   drop buysell price prin netprice qty;                    
run;                                                        
                                                            
      /* Sort the match data set by trade date.           */
   proc sort data=tradelib.match;                           
      by tdate;


/**********************************************************************/


data tradelib.open;                                   
   set tradelib.match(where=(carry='c'));             
proc print;                                           
   title 'Open Positions Carried Forward';            
   var tdate broker tvol descr comm buyprice sellpric;
   format tdate MMDDYY8.;                             

   /* Create the closed position data set.                */
data tradelib.closed;                                       
                                                            
     /* Read the matched trades. After the last one has   */
     /* been seen, make the variable EOF have a value     */
     /* of 1.                                             */
  set tradelib.match  end=eof;                              
                                                            
     /* Create date variables bdate and sdate             */
     /* for printing.                                     */
  length bdate $ 8 sdate $ 8;                               
                                                            
  if carry=' '         /* Omit positions carried forward. */
  then do;                                                  
                                                            
     retain gnet  0  /* grand total net profit or loss    */
            tprof 0  /* grand total of all profitable     */
                     /* trades                            */
            tloss 0  /* grand total of all losing trades  */
            gcom  0; /* grand total commissions paid      */
                                                            
        /* Accumulate grand totals for                    */
        /* net profit and commissions                     */
     gnet=gnet+netgain;                                                                                                                                                                                                                                        
 
     gcom=gcom+totcomm;                                     
                                                            
        /* The close date is either the buy date or the   */
        /* sell date, whichever came later. This date is  */
        /* used to order the output report lines          */
        /* chronologically.                               */
     if buydate>selldate                                    
        then clodate=buydate;                               
        else clodate=selldate;                              
                                                            
        /* Create date variables for the report           */
        /* in format MM/DD/YY.                            */
     if buydate>0                                           
     then bdate=put(buydate, MMDDYY8.);                     
     else bdate=' ';                                        
     if selldate>0                                          
     then sdate=put(selldate, MMDDYY8.);                    
     else sdate=' ';                                                                                                                                                                                                                                           


/**********************************************************************/


     if netgain>0 then do;                                  
        netloss=0;                                          
        tprof=tprof+netgain;     /* accumulate net profit */
        end;                                                
     else do;                                               
        netloss=-netgain;                                   
        tloss=tloss+netgain;       /* accumulate net loss */

        netgain=0;                                          
        end;                                                
     output;                                                
     end;


/**********************************************************************/


  if eof then do;                                           
     put tprof= tloss= gnet=;                               
     end;                                                   
                                                            
     /* these variables no longer needed                  */
  drop grandcom grandnet;                                   
run;                                                                                                                                                                                                                                                           
 

/**********************************************************************/


TPROF=1454.79 TLOSS=-803.63 GNET=651.16   


/**********************************************************************/
                                                                                                                                                                                                                      

proc print;                           
   title 'Trader Performance Summary';
   var tvol descr bdate sdate sellpric
       buyprice netloss netgain gnet;                                                                                                                                                                                                                          


/**********************************************************************/
  
                                                                                                                                                                                                                    
proc sort data=tradelib.closed;                             
   by clodate;                                              
                                                            
proc print;                                                 
   title 'Schedule D Supplemental Capital Gains and Losses';
   var tvol descr bdate sdate sellpric                      
       buyprice netloss netgain;                            
run;                                                                                                                                                                                                                                                           
 

/**********************************************************************/