Hi, I need to estimated a regression for each industry-year based on the Fama and French 48-industry classification (i.e. ffi) for all industries with at least 20 observations in a given year. I used forvalues function, but it gave me an error message, no observation r(2000);


I used the following code:
egen ffigvkey=group(ffi fyear)
sort ffigvkey
egen count=count(ffigvkey),by(ffigvkey)
drop if count<20
drop count ffigvkey
egen ffigvkey=group(ffi fyear)
gen residualnew=.
gen Inv_statenew=.


sort gvkey fyear
sum ffigvkey
local k=r(max)
set more off
forvalues i=1(1)`k'{
qui reg Investment L.Sales if ffigvkey==`i'
qui predict res if ffigvkey==`i', res //save the residuals
qui replace residualnew=res if ffigvkey==`i'
qui replace Inv_statenew=xtile(residualnew) if ffigvkey==`i',n(4) //classify residuals into quartiles.
qui recode Inv_statenew (2/3=2) (4=3) if ffigvkey==`i', gen(Inv_dumnew) //reclassify the quartiles: bottom quartile is classified as 1, top quartile is classified as 3, and the middle two quartiles are classified as the benchmark. (*will perform a multinomial logit model in future analysis)
qui drop res
di `i' " / " `k'
}

Here is my data:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long gvkey double fyear float(ffi Investment Sales ffigvkey residualnew Inv_statenew)
1003 1987 42         .           . 1157 . .
1003 1988 42 1.8451564   -.1217475 1158 . .
1003 1989 42  .4176904   -.4127347 1159 . .
1004 1987 41         .           . 1125 . .
1004 1988 41  4.424316   .16891037 1126 . .
1004 1989 41  5.129759    .0947805 1127 . .
1004 1990 41 2.1594715   .04870357 1128 . .
1004 1991 41 2.0970738  -.09406441 1129 . .
1004 1992 41         .  -.09434837 1130 . .
1004 1993 41         .   .06524374 1131 . .
1004 1994 41         .   .10702777 1132 . .
1004 1995 41         .   .11873193 1133 . .
1004 1996 41         .   .16700925 1134 . .
1004 1997 41         .    .3271438 1135 . .
1004 1998 41         .   .17377445 1136 . .
1004 1999 41         .    .1157874 1137 . .
1004 2000 41         .   -.1465129 1138 . .
1004 2001 41  3.296127   -.2694111 1139 . .
1004 2002 41         .  -.05070132 1140 . .
1004 2003 41         .   .07524034 1141 . .
1004 2004 41         .   .14708003 1142 . .
1004 2005 41         .   .19982135 1143 . .
1004 2006 41         .   .18264563 1144 . .
1004 2007 41         .    .3050881 1145 . .
1004 2008 41         .   .02820165 1146 . .
1004 2009 41         .  -.05043976 1147 . .
1004 2010 41         .    .3133015 1148 . .
1004 2011 41         .    .1682166 1149 . .
1004 2012 41         .   .04463827 1150 . .
1004 2013 41         .  -.06095704 1151 . .
1004 2014 41  .3182541   -.2165602 1152 . .
1004 2015 41         .   .04284012 1153 . .
1004 2016 41         .  .063154094 1154 . .
1004 2017 41         .  -.01091876 1155 . .
1004 2018 41         .    .1735972 1156 . .
1009 1987 20         .           .  559 . .
1009 1988 20 -6.512972    .4536573  560 . .
1009 1989 20  62.37898    .2803406  561 . .
1009 1990 20  21.05485    .1136701  562 . .
1009 1991 20         .   -.1061152  563 . .
1009 1992 20 14.117382    .4192204  564 . .
1009 1993 20  44.60168    .4075904  565 . .
1009 1994 20 34.507866    .2731067  566 . .
1011 1987 32         .           .  850 . .
1011 1988 32         .  -.04851158  851 . .
1011 1989 32         .   .26960215  852 . .
1011 1990 32  16.79198   .10587162  853 . .
1011 1991 32  7.014388    .2283356  854 . .
1011 1992 32  23.91055   .04456887  855 . .
1011 1993 32  43.48386    .3915094  856 . .
1011 1994 32 177.79024    1.733282  857 . .
1012 1987 19         .           .  527 . .
1012 1988 19  35.42532   .27728367  528 . .
1012 1989 19  6.540933   .28680688  529 . .
1013 1987 36         .           .  979 . .
1013 1988 36         .    .0778423  980 . .
1013 1989 36         .   .09194227  981 . .
1013 1990 36         .    .3229016  982 . .
1013 1991 36  58.44329    .1310113  983 . .
1013 1992 36         .   .07710685  984 . .
1013 1993 36         .   .15678555  985 . .
1013 1994 36         .   .22565676  986 . .
1013 1995 36         .     .306388  987 . .
1013 1996 36         .    .4124495  988 . .
1013 1997 36         .    .4063253  989 . .
1013 1998 36         .    .1848323  990 . .
1013 1999 36         .    .3966643  991 . .
1013 2000 36         .    .7062742  992 . .
1013 2001 36 14.056164   -.2691992  993 . .
1013 2002 36         .  -.56396705  994 . .
1013 2003 36 15.399405  -.26200247  995 . .
1013 2004 36 27.249596  .014355923  996 . .
1013 2005 36  18.08697    .4907561  997 . .
1013 2006 36  6.807817    .0963907  998 . .
1013 2007 36  6.863597   .03143771  999 . .
1013 2008 36 18.353355    .1014975 1000 . .
1013 2009 36  4.971369   -.3156413 1001 . .
1013 2010 36  7.606431    .1604294 1002 . .
1015 1987 36         .           .  979 . .
1016 1987 21         .           .  578 . .
1017 1987 36         .           .  979 . .
1017 1988 36  8.984076   .14510562  980 . .
1017 1989 36  9.409397  -.11560837  981 . .
1017 1990 36  9.571166    .1906503  982 . .
1017 1991 36   6.21402 -.028740173  983 . .
1017 1992 36  9.946867  -.18993376  984 . .
1017 1993 36  7.455995    .0936564  985 . .
1017 1994 36  6.233281  .021348586  986 . .
1018 1987  9         .           .  220 . .
1019 1987 34         .           .  914 . .
1019 1988 34 16.942738    .1202394  915 . .
1019 1989 34         .   .05021855  916 . .
1019 1990 34         .   .04823345  917 . .
1019 1991 34         .   .03930825  918 . .
1019 1992 34         .   .02619068  919 . .
1019 1993 34         .   .03921406  920 . .
1019 1994 34         .   .02515623  921 . .
1019 1995 34         .  .067443214  922 . .
1019 1996 34         .  -.01207624  923 . .
1019 1997 34         .   .09499264  924 . .
end
label values ffi ffi
label def ffi 9 "Consumer Goods", modify
label def ffi 19 "Steel Works Etc", modify
label def ffi 20 "Fabricated Products", modify
label def ffi 21 "Machinery", modify
label def ffi 32 "Communication", modify
label def ffi 34 "Business Services", modify
label def ffi 36 "Electronic Equipment", modify
label def ffi 41 "Wholesale", modify
label def ffi 42 "Retail", modify