Hi,

I have been struggling with coding this. I try to run to 2 loops but it failed. So, I hope someone can help me out.

My data has, say, 2 groups (1&2). I'd like to regress coef_std on j_ln in a subsample of all observations which has id< current id, within each group. Then, I take coefficients on j_ln from that regression, assign into all observations in this subsample, which is called beta_pre

For example:
1) at id=3, group 1: I want to regress coef_std on j_ln of all observations with id=1 and id=2, then I take the coefficient on j_ln of that regression and assign into beta_pre of all observations with id=1 and id=2
2) at id=22, group 2: I want to regress coef_std on j_ln of all observations with id=20, and id =21 (which is only in group 2, not from id=1 to id=19), then I take the coefficient on j_ln of that regression and assign into beta_pre of all observations with id=20 and id=21

I hope I am clear about that.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear

input byte Groups float(coef_std j_ln id beta_pre)
1   -.1092731    .998055 1 .
1  -.10569589    .998055 1 .
1  -.05203765  -.8347107 2 .
1 -.005533839  -.8347107 2 .
1  -.05203765  -.8347107 2 .
1  -.05203765  -.8347107 2 .
1  -.10211867  -.8347107 2 .
1  -.02341992  -.8347107 2 .
1  -.05561486  -.8347107 2 .
1  -.05919208  -.8347107 2 .
1  -.11642753  -.8347107 2 .
1  -.10569589  -.8347107 2 .
1  -.12000475  -.8347107 3 .
1   -.1987035  -.8347107 3 .
1   .16974975  1.7107302 4 .
1  -.06459367  1.6678954 4 .
1   -.2022807  1.7107302 4 .
1  -.09496424  -2.748872 4 .
1   .08389656  -2.748872 4 .
1   .04096997  1.7107302 4 .
1  -.06051565  1.6678954 4 .
1  -.20442705  -2.465104 5 .
1   -.1142812  -2.465104 5 .
1   -.0463141  -2.465104 5 .
1   -.4634175  -2.465104 5 .
1    -.154346  1.1565667 6 .
1   -.0878098  -4.828314 6 .
1  -.13431361  -4.828314 6 .
1   -.1457607  1.1565667 6 .
1  -.12000475  -4.828314 6 .
1  -.12715918  -4.828314 6 .
1  -.21480097  1.1565667 6 .
1   -.3167516 -1.6144505 6 .
1  -.03057435  -4.828314 6 .
1  -.17008577  -4.828314 6 .
1 -.034151565  1.1565667 6 .
1  -.16185817  1.1565667 6 .
1   -.1536306  1.1565667 6 .
1  -.14146805 -1.6144505 6 .
1  -.07707816  -4.828314 6 .
1  -.13431361  -4.828314 6 .
2 -.27092713   2.625393 20 .
2  -.2986687   2.625393 20 .
2  -.3726464   2.625393 20 .
2  .08046687   2.625393 20 .
2 -.20619665   2.625393 20 .
2 -.13221897   2.625393 20 .
2  -.3217868  1.1565667 21 .
2  -.9237801  1.1565667 21 .
2 -.12389648  1.1565667 21 .
2   .3412382  1.1565667 21 .
2 -.51227933  1.1565667 21 .
2   .9450811  1.1565667 21 .
2   .0157364  1.7107302 22 .
2   .8017493  1.7107302 22 .
2 -.01200523  1.7107302 22 .
2   .5058386  1.7107302 22 .
2  .11745571  1.7107302 22 .
2 -.04899407  1.7107302 22 .
2  .11745571  1.7107302 22 .
2  .05272524  1.7107302 22 .
2  .22842224  1.7107302 22 .
2 -.28942153  1.7107302 23 .
2 -.02125244 -.58519006 23 .
2 -.12297176  1.7107302 23 .
2  -.4373769  1.7107302 23 .
2 -.13221897  1.7107302 23 .
2   -.178455  1.7107302 23 .
2 -.13221897  1.7107302 23 .
2 -.12297176  1.7107302 23 .
2 -.08598291  1.7107302 23 .
2   .5613218  1.7107302 23 .
2  -.1692078  1.7107302 23 .
2 -.27092713 -.58519006 23 .
2 -.03974686   1.791593 24 .
2  .05272524   1.791593 24 .
2  -.0767357  1.1565667 24 .
2  -.3726464   1.791593 24 .
2  -.2431855  1.1565667 24 .
2 -.27092713   1.791593 24 .

end

Thank you!