I am trying to do the following:

Code:
        
local i 10
local j 20
quietly summ n    
local t round(`r(max)'/10)
forval p = 1/`t'{
            sort Date Amihud_Liquidity
            bysort Date: egen Portfolio`p'= total(Amihud_Liquidity) if Portfolio==. & n>`i' & n<=`j'
            replace Portfolio = Portfolio`p' if Portfolio==.
            drop Portfolio`p'
            
            local i = `i'+10
            local j = `j'+10
}
But because of the way I have defined the local t, the loop doesn't execute, even though when I display local t, it returns the correct value (so the local is working fine). I'm sure there's a very basic explanation for this, but I just can't get it. This is a nested loop that needs to be performed on several files, so r(max) will be different each time.

I basically need the Portfolio variable to be updated every 10 observations. Each Date appears 10+ times, with 10+ values for Amihud_Liquidity.
I need the first 10 values if Amihud_Liquidity added and the constant stored in the first 10 observations for Portfolio. Then the sum of the next 10 Liquidities stored in the next 10 of Portfolio and so on.

n was generated by:
Code:
bysort Date: gen n = _n