I sincerely thank each of you for sparing your valuable time to help me out. I tried Hash( ) approach, but my hard disk ran out of space. I am still trying to implement ghastly_kitten' suggestion. Daniel's code is very efficient (takes about 1 minute instead of 24+ hours) and it works correctly in the small data set, but for some reasons, in my real data set, it is creating more than necessary observations (961,896 is the number of rows in "return", but Daniel's code is creating 47,132,904 observations). Arthur's modification solves that problem, but the average for a given firmid still contains that firm's returns. Below is Daniel's code (modified by Arthur and slightly changed by me). The only remaining task is to exclude a stock's own return in the computation of its corresponding average return (a stock's average return should be calculated by averaging all different stocks in different portfolios. In other words, for portid=1 and firmid=100, I cannot have any stocks from portid=1 and I must also exclude firmid=100 since the empirical design allow a firmid to exist in more than one portid) The final result should match Daniel's output. I will be happy to provide more detail if you would like. I am also trying to figure out how to impose the condition that a firm's return should be excluded from its average (taking me some time to figure out how Daniel did and how I can incorporate that into Arthur's code), but I would really appreciate your help in figuring this out. proc sql noprint; create table want as select distinct a.portid,a.firmid,a.myear,a.return,b.sumreturn, b.cntreturn,bportid from return as a left join (select portid as bportid, myear, sum(return) as sumreturn, count(return) as cntreturn from return group by myear,bportid) as b on a.myear=b.myear where a.portid ne bportid *this is where I made a slight correction; order by firmid,myear,portid ; quit; data want; set want; by firmid myear; if not (first.myear and last.myear) then do; sumreturn=sumreturn-return; cntreturn=cntreturn-1; end; average=sumreturn/cntreturn; run; proc sort data=want; by portid myear firmid; run;
... View more