Can you not then just coalesce the values, sorry am not really clear on what relation p2 or p3 is to the buy or sell column, so I will assume they relate to sell proc sql; create table WANT as select CUSPID, sum(BUY) as BUY, sum(coalesce(P2,P3,SELL)) as SELL, sum(BUY) - sum(coalesce(P2,P3,SELL) as BUY_SELL from HAVE group by CUSPID; quit; Another tip is to normalise your data, i.e. rather than having columns for each bit, have rows, and an identifier column: CUSPID DATE ID RESULT 0011 11jan12 BUY 0.3453525 0011 11jan12 P2 0.45564 ... It makes it easier to do aggregates.
... View more