Hi all,
until now I have estimated intercepts and coefficients "asking" STATA to do it for each different firm, and year. That worked fine. What about if I want to estimate again interecepts and coefficients for each different firm BUT considering one precise year and also the observations of the previous year? For example for id_firm=1 in 1998 to consider both obs belonging to 1998 and 1997 and so on for 1999 to consider 1999 and 1998, this for all the different firms.

Thanks!

Until now I have used this command, but of course now is anymore appropiated:

egen group_COMPANY = group(id_firm)
egen group_YEAR = group(Year)
codebook group_COMPANY group_YEAR
gen BetaCAPM=.
gen InterceptCAPM=.
forval c= 1/2 {
forval a= 1/4 {
capture reg Return_firm_Rf MktRF if group_COMPANY ==`c' & group_YEAR==`a'
if !_rc {
gen beta`c'`a' = _b[MktRF] if group_COMPANY ==`c' & group_YEAR==`a'
gen intercept`c'`a' = _b[_cons] if group_COMPANY ==`c' & group_YEAR==`a'
replace Beta = beta`c'`a' if group_COMPANY ==`c' & group_YEAR==`a'
replace Intercept = intercept`c'`a' if group_COMPANY ==`c' & group_YEAR==`a'
capture drop beta`c'`a' intercept`c'`a'
}
}
}


----------------------- copy starting from the next line -----------------------
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input int id_day str8 day double(MktRF SMB HML RF) int Year byte(Month Day) str12 TargetISINnumber double Priceclose byte id_firm double Priceclose_lag1 float(Return_firm Return_firm_Rf)
  1 "19970102"  -.75   .05   .13  .02 1997 1  2 "US0003602069" .417005849 1          .            .            .
  2 "19970103"  1.39  -.32  -.78  .02 1997 1  3 "US0003602069" .417005849 1 .417005849            0         -.02
  3 "19970106"   .02   .11   .05  .02 1997 1  6 "US0003602069" .422492768 1 .417005849   .013157895  -.006842105
  4 "19970107"   .72  -.02  -.52  .02 1997 1  7 "US0003602069" .427979687 1 .422492768   .012987013  -.007012987
  5 "19970108"  -.44    .5   .53  .02 1997 1  8 "US0003602069" .460901202 1 .427979687    .07692308    .05692308
  6 "19970109"   .72  -.38  -.15  .02 1997 1  9 "US0003602069" .466388121 1 .460901202   .011904762  -.008095237
  7 "19970110"    .5  -.17  -.37  .02 1997 1 10 "US0003602069"  .47187504 1 .466388121   .011764706  -.008235293
  8 "19970113"  -.07   .09   .27  .02 1997 1 13 "US0003602069" .669404126 1  .47187504     .4186046     .3986046
  9 "19970114"  1.11  -.85   .04  .02 1997 1 14 "US0003602069" .570639583 1 .669404126   -.14754099     -.167541
 10 "19970115"  -.22   .04   .47  .02 1997 1 15 "US0003602069" .537718069 1 .570639583   -.05769231    -.0776923
 11 "19970116"   .25  -.21  -.33  .02 1997 1 16 "US0003602069"  .52674423 1 .537718069  -.020408165   -.04040816
 12 "19970117"   .72  -.28  -.26  .02 1997 1 17 "US0003602069" .548691907 1  .52674423    .04166667    .02166667
 13 "19970120"   .26   .21  -.41  .02 1997 1 20 "US0003602069" .576126502 1 .548691907          .05          .03
 14 "19970121"   .59  -.53  -.21  .02 1997 1 21 "US0003602069"  .58710034 1 .576126502    .01904762 -.0009523821
 15 "19970122"   .44  -.41  -.07  .02 1997 1 22 "US0003602069" .609048016 1  .58710034    .03738318   .017383177
 16 "19970123"  -.85   .91   .37  .02 1997 1 23 "US0003602069" .598074178 1 .609048016  -.018018018   -.03801802
 17 "19970124"  -.97   .35   .29  .02 1997 1 24 "US0003602069" .592587259 1 .598074178 -.0091743115   -.02917431
 18 "19970127"  -.72   .08   .34  .02 1997 1 27 "US0003602069"  .58710034 1 .592587259  -.009259259   -.02925926
 19 "19970128"   .01   .41  -.22  .02 1997 1 28 "US0003602069" .609048016 1  .58710034    .03738318   .017383177
 20 "19970129"   .68  -.73  -.03  .02 1997 1 29 "US0003602069" .581613421 1 .609048016   -.04504504  -.065045044
 21 "19970130"  1.21  -.67  -.42  .02 1997 1 30 "US0003602069" .570639583 1 .581613421  -.018867925   -.03886792
 22 "19970131"   .31   .01  -.35  .02 1997 1 31 "US0003602069" .570639583 1 .570639583            0         -.02
 23 "19970203"  -.02   .04   .38  .02 1997 2  3 "US0003602069" .592587259 1 .570639583    .03846154   .018461537
 24 "19970204"   .12  -.54   .28  .02 1997 2  4 "US0003602069" .581613421 1 .592587259  -.018518519   -.03851852
 25 "19970205" -1.28   .47   .77  .02 1997 2  5 "US0003602069" .570639583 1 .581613421  -.018867925   -.03886792
 26 "19970206"   .24  -.23  -.09  .02 1997 2  6 "US0003602069" .559665745 1 .570639583   -.01923077   -.03923077
 27 "19970207"   .96  -.79  -.04  .02 1997 2  7 "US0003602069" .537718069 1 .559665745   -.03921568   -.05921568
 28 "19970210"  -.65  -.08   .81  .02 1997 2 10 "US0003602069"  .52674423 1 .537718069  -.020408165   -.04040816
 29 "19970211"   .32   -.9   .45  .02 1997 2 11 "US0003602069"  .52674423 1  .52674423            0         -.02
 30 "19970212"  1.52  -.79  -.67  .02 1997 2 12 "US0003602069"  .52674423 1  .52674423            0         -.02
 31 "19970213"  1.02  -.54   .16  .02 1997 2 13 "US0003602069" .537718069 1  .52674423   .020833336  .0008333358
 32 "19970214"  -.22   .29   .32  .02 1997 2 14 "US0003602069"  .52674423 1 .537718069  -.020408165   -.04040816
 33 "19970218"   .71  -.52   .27  .02 1997 2 18 "US0003602069" .515770392 1  .52674423  -.020833334   -.04083334
 34 "19970219"  -.32   .41   .31  .02 1997 2 19 "US0003602069" .493822716 1 .515770392   -.04255319   -.06255319
 35 "19970220" -1.17   .41   .42  .02 1997 2 20 "US0003602069" .510283473 1 .493822716    .03333333   .013333332
 36 "19970221"  -.26  -.05   .64  .02 1997 2 21 "US0003602069" .515770392 1 .510283473   .010752688  -.009247311
 37 "19970224"   .85  -.78   .03  .02 1997 2 24 "US0003602069" .521257311 1 .515770392   .010638298  -.009361702
 38 "19970225"   .25  -.18   .17  .02 1997 2 25 "US0003602069" .504796554 1 .521257311  -.031578947   -.05157895
 39 "19970226"  -.75   .17   .11  .02 1997 2 26 "US0003602069" .548691907 1 .504796554    .08695652    .06695652
 40 "19970227" -1.27   .61   .76  .02 1997 2 27 "US0003602069" .570639583 1 .548691907          .04          .02
 41 "19970228"  -.49   .04   .15  .02 1997 2 28 "US0003602069" .609048016 1 .570639583    .06730769    .04730769
 42 "19970303"   .39  -.47   .13 .021 1997 3  3 "US0003602069" .598074178 1 .609048016  -.018018018   -.03901802
 43 "19970304"  -.29   .66   .23 .021 1997 3  4 "US0003602069" .603561097 1 .598074178  .0091743115  -.011825688
 44 "19970305"  1.17  -.51   -.5 .021 1997 3  5 "US0003602069" .625508774 1 .603561097    .03636364    .01536364
 45 "19970306"  -.31   .09   .47 .021 1997 3  6 "US0003602069"  .64745645 1 .625508774    .03508772    .01408772
 46 "19970307"   .67  -.59   .23 .021 1997 3  7 "US0003602069" .630995693 1  .64745645   -.02542373   -.04642373
 47 "19970310"   .85  -.45   .03 .021 1997 3 10 "US0003602069" .636482612 1 .630995693   .008695652  -.012304348
 48 "19970311"  -.24   .39   .27 .021 1997 3 11 "US0003602069" .630995693 1 .636482612   -.00862069   -.02962069
 49 "19970312"  -.79  -.07   .77 .021 1997 3 12 "US0003602069"  .64745645 1 .630995693   .026086956   .005086956
 50 "19970313" -1.66   .67   .01 .021 1997 3 13 "US0003602069" .636482612 1  .64745645  -.016949153   -.03794915
 51 "19970314"   .31  -.42  -.11 .021 1997 3 14 "US0003602069" .663917207 1 .636482612    .04310345    .02210345
 52 "19970317"  -.11  -.84   .62 .021 1997 3 17 "US0003602069" .636482612 1 .663917207   -.04132231   -.06232231
 53 "19970318"  -.77    .3   .16 .021 1997 3 18 "US0003602069" .636482612 1 .636482612            0        -.021
 54 "19970319"  -.65  -.71    .8 .021 1997 3 19 "US0003602069" .620021855 1 .636482612   -.02586207   -.04686207
 55 "19970320"   -.2   .48  -.19 .021 1997 3 20 "US0003602069" .625508774 1 .620021855  .0088495575  -.012150442
 56 "19970321"   .09  -.37   .22 .021 1997 3 21 "US0003602069" .641969531 1 .625508774    .02631579    .00531579
 57 "19970324"   .47 -1.45   .68 .021 1997 3 24 "US0003602069" .641969531 1 .641969531            0        -.021
 58 "19970325"  -.08   .41  -.32 .021 1997 3 25 "US0003602069" .620021855 1 .641969531  -.034188032   -.05518803
 59 "19970326"   .23   .54  -.83 .021 1997 3 26 "US0003602069" .614534936 1 .620021855 -.0088495575   -.02984956
 60 "19970327"  -1.9  1.18   .26 .021 1997 3 27 "US0003602069" .592587259 1 .614534936  -.035714287   -.05671429
 61 "19970331" -2.26   .67  1.05 .021 1997 3 31 "US0003602069" .609048016 1 .592587259    .02777778   .006777778
 62 "19970401"   .17  -.89   .34  .02 1997 4  1 "US0003602069" .592587259 1 .609048016  -.027027026   -.04702703
 63 "19970402" -1.17   .44   .12  .02 1997 4  2 "US0003602069"  .58710034 1 .592587259  -.009259259   -.02925926
 64 "19970403"  -.02  -.34  -.36  .02 1997 4  3 "US0003602069" .603561097 1  .58710034    .02803738   .008037383
 65 "19970404"  1.15   .27 -1.44  .02 1997 4  4 "US0003602069" .598074178 1 .603561097  -.009090909   -.02909091
 66 "19970407"   .77   .27   -.2  .02 1997 4  7 "US0003602069"  .58710034 1 .598074178  -.018348623   -.03834862
 67 "19970408"   .43  -.38   .18  .02 1997 4  8 "US0003602069" .581613421 1  .58710034  -.009345794  -.029345794
 68 "19970409"  -.58    .9   .33  .02 1997 4  9 "US0003602069" .620021855 1 .581613421    .06603774    .04603774
 69 "19970410"  -.42     0   .36  .02 1997 4 10 "US0003602069" .618650125 1 .620021855 -.0022123898   -.02221239
 70 "19970411" -2.48   .95   .45  .02 1997 4 11 "US0003602069" .625508774 1 .618650125   .011086475  -.008913524
 71 "19970414"   .55  -.67  -.31  .02 1997 4 14 "US0003602069" .614534936 1 .625508774   -.01754386   -.03754386
 72 "19970415"  1.19 -1.04   .24  .02 1997 4 15 "US0003602069" .609048016 1 .614534936  -.008928573   -.02892857
 73 "19970416"   .81 -1.02   .23  .02 1997 4 16 "US0003602069" .603561097 1 .609048016  -.009009009   -.02900901
 74 "19970417"  -.11   .13   .31  .02 1997 4 17 "US0003602069" .603561097 1 .603561097            0         -.02
 75 "19970418"   .42  -.09  -.09  .02 1997 4 18 "US0003602069" .603561097 1 .603561097            0         -.02
 76 "19970421"  -.99   -.1   .44  .02 1997 4 21 "US0003602069" .543204988 1 .603561097          -.1         -.12
 77 "19970422"   1.4  -1.6  -.05  .02 1997 4 22 "US0003602069" .581613421 1 .543204988    .07070707    .05070707
 78 "19970423"  -.03  -.05  -.19  .02 1997 4 23 "US0003602069" .559665745 1 .581613421   -.03773585   -.05773585
 79 "19970424"  -.22   .43  -.08  .02 1997 4 24 "US0003602069" .559665745 1 .559665745            0         -.02
 80 "19970425"  -.95   .16   .39  .02 1997 4 25 "US0003602069" .559665745 1 .559665745            0         -.02
 81 "19970428"   .81 -1.12   .19  .02 1997 4 28 "US0003602069" .504796554 1 .559665745   -.09803922   -.11803922
 82 "19970429"  2.41  -1.5  -.39  .02 1997 4 29 "US0003602069" .537718069 1 .504796554    .06521739    .04521739
 83 "19970430"   .93  -.43  -.45  .02 1997 4 30 "US0003602069" .570639583 1 .537718069    .06122449    .04122449
 84 "19970501"   -.1  1.02  -.26 .023 1997 5  1 "US0003602069" .592587259 1 .570639583    .03846154   .015461536
 85 "19970502"  1.93   .53  -.85 .023 1997 5  2 "US0003602069" .592587259 1 .592587259            0        -.023
 86 "19970505"  2.16   .14 -1.61 .023 1997 5  5 "US0003602069" .625508774 1 .592587259    .05555556   .032555558
 87 "19970506"  -.31   .02   .63 .023 1997 5  6 "US0003602069" .609048016 1 .625508774   -.02631579   -.04931579
 88 "19970507" -1.18  1.21  -.03 .023 1997 5  7 "US0003602069" .614534936 1 .609048016    .00900901   -.01399099
 89 "19970508"   .41  -.55  -.07 .023 1997 5  8 "US0003602069" .614534936 1 .614534936            0        -.023
 90 "19970509"   .51  -.07   .12 .023 1997 5  9 "US0003602069" .636482612 1 .614534936   .035714284   .012714284
 91 "19970512"  1.29  -.74  -.19 .023 1997 5 12 "US0003602069" .603561097 1 .636482612   -.05172414   -.07472414
 92 "19970513"  -.45   .45     0 .023 1997 5 13 "US0003602069" .603561097 1 .603561097            0        -.023
 93 "19970514"   .35  -.24   .43 .023 1997 5 14 "US0003602069" .614534936 1 .603561097    .01818182   -.00481818
 94 "19970515"   .58  -.08  -.85 .023 1997 5 15 "US0003602069" .603561097 1 .614534936  -.017857144   -.04085714
 95 "19970516" -1.13  1.05   .46 .023 1997 5 16 "US0003602069" .603561097 1 .603561097            0        -.023
 96 "19970519"   .27     0  -.18 .023 1997 5 19 "US0003602069" .625508774 1 .603561097    .03636364    .01336364
 97 "19970520"   .91  -.59  -.19 .023 1997 5 20 "US0003602069" .603561097 1 .625508774   -.03508772   -.05808772
 98 "19970521"  -.13   .83  -.69 .023 1997 5 21 "US0003602069" .625508774 1 .603561097    .03636364    .01336364
 99 "19970522"  -.27   .56   .45 .023 1997 5 22 "US0003602069"  .64745645 1 .625508774    .03508772    .01208772
100 "19970523"  1.26  -.09  -.49 .023 1997 5 23 "US0003602069" .636482612 1  .64745645  -.016949153   -.03994915
end
------------------ copy up to and including the previous line ------------------