I have the following loop, which intends to calculated WLAT, that is average returns of to top 3 firms minus average returns of bottom three firms. I have to use the aWT`i' and aLT`i' becuase I want to spread the results across all observations. Can this code of reduce or improved to increase efficiency in terms of speed or avoiding the generation of aWT`i' and aLT`i' . Sample data is attached.
Code:
forval i=1(1)600{
local j=`i'+1
qui egen LT`i'=mean( R`j') if pf`i'<4
qui egen WT`i'=mean( R`j') if pf`i'>7& pf`i'!=.
qui egen aWT`i'=mean( WT`i')
qui egen aLT`i'=mean( LT`i')
qui gen WLAT`i'= aWT`i'- aLT`i'
}
keep WLAT*

Comment