Dear stata members, I am trying to calculate the squared sum of all dyadic values in a group using
Code:
forval
and something is going wrong (probably I am not using forval correctly.

Here is how data for one of the groups look like:
group id a1 teamsize last_i sigma_SiSj_2
x 252 5 7 2 4200 calculated as := (5-40)^2 + (5-1)^2 + (5-2)^2 + (5-2) + (5-59)^2 + (5-10)^2
x 11 40 7 2 6895
x 231 1 7 2 4984
x 601 2 7 2 4767
x 242 2 7 2 4767
x 615 59 7 2 15540
x 14 10 7 2 3535
The last column (sigma_SiSj_2) is the DESIRED value, the one I am trying to calculate using
Code:
forval
, last_i is the control var i added trying to understand what is going on, and according to this value, the loop breaks after 2nd iteration


Here is the code I run (max teamsize is 64)

Code:
forval i = 1/64{

if `i' <= teamsize{
by group: replace sigma_SiSj_2 = sigma_SiSj_2 + (a1 - a1[`i'])^2
replace last_i=`i'

} 
*/ IF
} 
*/ FORVAL i = 1/64

What/where is my error?

Thank you in advance,

C.