Dear Statalists,

I hope anyone can help me with my regressions with loops because I am quite new to use multiple loops. I have:

- 2 different dependent variables: a and b
- 5 different independent variables: c, d, f, g and h
- One same set of control variables
- And I have another variable z, which is divided into terciles

I would like to run each following regression based on the terciles of z:

reg a c controls
reg a d controls
reg a f controls
reg a g controls
reg a h controls

reg b c controls
reg b d controls
reg b f controls
reg b g controls
reg b h controls

Also, I want to use outreg2 to extract the results in order (for example, reg a c controls (1st tercile), reg a c controls (2nd tercile) and reg a c controls (3rd tercile). Then reg a d controls (1st tercile), reg a d controls (2nd tercile) and reg a d controls (3rd tercile) ...........................reg b h controls (1st tercile), reg b h controls (2nd tercile) and reg b h controls (3rd tercile).

I have tried to write the code like this, but it gives me a table with over 100 columns. Can anyone help me, please?

I really appreciate your help.

Code:
xtset id year

xtile z_tercile = z,nquantiles(3)

global controls = "x1 x2 x3 x4 x5"

local indvar1 F.a
local indvar2 F.b

local dvar1 c
local dvar2 d
local dvar3 f
local dvar4 g
local dvar5 h

forv i=1/3{
    forv d=1/5{
        forv j=1/2{
        xtreg `indvar`j'' `dvar`d'' $controls if z_tercile==`i', fe
        outreg2 using table.xls, append stats(coef se) dec(4) sortvar(c d f g h $controls)
        }
    }
}
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long id double year float(a b c d f g h x1 x2 x3 x4) double x5 float z
1 2003    .05740558   .6179267  .0002737476 .07694252           0 1.7885895e-18  -.7820968  4.625806          0 2.6390574  .7210243     0  .6931472
1 2004     .0649916   .6347956 .00025634453 .07694128           0 1.7885895e-18  -.7893308  4.590917   .0101875  2.708233  .7519287     0 1.0986123
1 2005    .05914634  .43740895 .00023952096 .08335163           0 1.7885895e-18   -.796324 4.6095705 .014796753   2.77276  .8061169     0 1.3862944
1 2006    .04168578   .3297939  .0002274278 .07693923           0 1.7885895e-18  -.8013507 4.6997027 .015178077 2.8333745  .8484832    .2 1.0986123
1 2007   .017250383  .26725262  .0006476684  .1538206           0 1.3404966e-11  -.6266699 4.7348423 .018105585  2.890524  .8426654 .2667 1.3862944
1 2008   .016824573  .26758727   .000617538  .1573849           0 1.6064574e-16  -.6391943  4.778876  .01830997 2.9447274  .8562811   .32  .6931472
1 2009   .010607123  .18055393  .0006076565  .1680738           0 1.0341008e-16   -.643302  4.846248 .019844953  2.996006  .9456088   .34  1.609438
1 2010   .012180088   .2050075  .0008130081  .1687243  .000598591  6.644726e-18  -.5579432  4.835881 .022461114  3.044783  .8313994   .54  .6931472
1 2011   .018037405  .20919257  .0008130081  .1687243  .000598591  6.644726e-18  -.5579432  4.905014  .02681849 3.0912914  .8649291   .24  .6931472
1 2012   .015667645  .16122043  .0002183883 .07693751           0 1.7885895e-18  -.8051081  4.947325 .018605324 3.1358516  .8229198   .36  .6931472
1 2013   .006773706  .10351888  .0002254283 .07693751           0 1.7885895e-18  -.8021818  5.011819   .0241362  3.178396  .7890258   .25 1.0986123
1 2014   .004964413  .10086903  .0002254283 .07693751           0 1.7885895e-18  -.8021818   5.05162 .027025057 3.2192044  .8287393 .1767  .6931472
1 2015   .004425053  .08169895 .00022727273  .0833507           0 1.7885895e-18  -.8014152  5.024065 .032209024 3.2584126  .8099487   .22 1.0986123
1 2016  .0032249265   .0959488 .00022815423 .07693928           0 1.7885895e-18  -.8010487  5.080141  .04677815  3.296243  .8995772   .24 1.0986123
1 2017  .0023096246   .1461774  .0002428953 .07694033           0 1.7885895e-18  -.7949212  5.175416  .04380349  3.332596  .9172587   .26 1.3862944
2 2003    .02612241   .3924782  .0010949904  .1778481  .008772796   .0039610784  -.4407313  6.564267          0 3.4875846  .5732008     0 1.0986123
2 2004    .01289697   .3268641  .0025634454  .1966329   .17614095    .012056245   .1696654  6.530892          0 3.5177824  .5656238     0  .6931472
2 2005  .0045446623  .20836908  .0028742515 .20228693    .2470284 2.8241735e-07   .2988595  6.763181          0  3.547016   .677748     0  .6931472
2 2006   .001798926  .11524795   .002501706 .19851913   .13199544  7.290945e-09  .14400117  6.804937          0  3.575419  .6036456     0  1.609438
2 2007  .0013387145  .15850027   .001943005 .20214716   .04211038  5.136419e-09   -.088236  7.030557          0  3.603038  .6151881     0 2.0794415
2 2008  .0038360446   .2009262   .001440922 .18784317  .015204014 1.5672164e-16 -.29693758  7.059974          0  3.629987  .5556599     0 2.3025851
2 2009  .0037338636   .0908347   .001215313 .19067666  .013338535 3.0150275e-16 -.39071685  7.108955          0  3.656158  .5589364     0 2.0794415
2 2010   .002989111 .066077255  .0014227643 .20549662   .05288613 1.9892816e-16  -.3044849  7.199551          0 3.6816616  .5592807  .075 2.0794415
2 2011   .003093391  .05334071  .0014227643 .20549662   .05288613 1.9892816e-16  -.3044849  7.411969          0  3.706531  .5291632    .3 2.0794415
2 2012   .003450312  .08073868   .001528718 .20599218    .0396458  3.823008e-10 -.26044324  7.349195          0 3.7308624  .4789042    .3  1.609438
2 2013   .002576775  .05417194  .0018034265 .19309625   .03811708  1.553727e-10  -.1462547  7.335104          0  3.754552   .505724    .3   1.94591
2 2014  .0023178835  .04430104  .0018034265 .19309625   .03811708  1.553727e-10  -.1462547   6.92325          0  3.777693 .48509565    .3 1.7917595
2 2015    .00206578   .0457977  .0013636363  .2025596  .010970575  8.650853e-09  -.3290634  6.847508          0 3.8003106    .61159    .3  1.609438
2 2016   .002769194  .04323253  .0009126169 .19533826   .01032933  4.674557e-08   -.516539  6.848845          0  3.822488  .6338278    .3  1.609438
2 2017  .0017740118 .032168075  .0009715813  .1715631   .02921264  2.270759e-16  -.4920285  6.811977          0 3.8441255 .50186956    .3 1.3862944
3 2005  .0005424603  .07031573   .006467066  .2123277    .2797776 1.4849527e-07  1.7922888   7.21312  .04664495 3.3255286  .4456343     0  2.944439
3 2006  .0004448615  .06746279   .005458267  .2112622   .28150406   3.93027e-07  1.3729603  7.216596  .04492987  3.360851  .4317414     0 3.0910425
3 2007 .00050151534  .09666108   .005829016   .216075   .27930954 2.6268037e-08  1.5270698  7.289632   .0394379  3.394968   .646658     0  2.995732
3 2008  .0015107996  .14773382   .005351997 .22141197   .21180776    5.8991e-17  1.3287855  7.392541  .04346694 3.4280484  .6540202     0  2.890372
3 2009  .0015849762  .16180916   .003038282   .218432   .10105354   3.08965e-17   .3670397  6.998148  .04860077  3.459983  .6479015     0  2.397895
3 2010 .00009596899  .08681683   .004878049 .21952525   .15408178 2.6053685e-17  1.1317782  7.055051  .04727026 3.4909296  .7483473     0   2.70805
4 2005     .0047207   .1713632  .0009580838 .19113675 .0040155784 1.0106709e-07  -.4976397  6.683342          0 3.6350596  .4340151  .415 1.3862944
4 2006   .003229072  .12769769  .0009097112 .19190817 .0010546603  3.887981e-08  -.5177469  6.755636          0    3.6611  .5074153  .435 1.3862944
4 2007  .0022369374  .15347514 .00021588946 .18585242           0  6.493791e-09  -.8061484  6.835525          0  3.686479  .4516935   .47 1.0986123
4 2008   .004859281   .1954047  .0016467682  .2070406   .05479844  1.952668e-18   -.211373  7.177894          0 3.7112975  .3155712  .495 1.0986123
4 2009   .004786891   .1041494  .0016204172  .2139545    .0225729  4.583826e-17 -.22232695  7.122264          0  3.735449 .28163427  .515  .6931472
4 2010  .0027998574  .05379133  .0010162601 .21389444  .015079642   6.77241e-17  -.4734577  7.104129          0 3.7590315 .29027265  .535 1.3862944
4 2011   .002873957   .0543122  .0010162601 .21389444  .015079642   6.77241e-17  -.4734577  7.256547          0 3.7820704 .29499155  .555  1.609438
4 2012  .0018365952   .0577327  .0006551649  .1818868  .015613783 1.4554608e-10  -.6235541  7.215377          0  3.804651 .28515923  .575 1.7917595
4 2013  .0019852163  .04267524  .0011271415  .1779668  .017036226 1.3531802e-10  -.4273669  7.297929          0  3.826674  .3124125  .595   1.94591
4 2014  .0016854556  .04595727  .0011271415  .1779668  .017036226 1.3531802e-10  -.4273669  7.293059          0  3.848222  .3202278  .615 2.0794415
4 2015  .0011345441  .04072658  .0020454545 .21651413    .1331242 1.0710759e-07  -.0456495  7.246783          0 3.8693156  .3250595  .635 2.0794415
4 2016  .0008595755 .033033803  .0015970797  .1959671   .10749505 1.2658522e-07 -.23202603  7.265352          0 3.8900294  .3412485  .655 1.7917595
4 2017  .0006994272  .02909028   .000728686 .16388026   .06197111  1.408617e-16  -.5929924  7.728491          0  3.910268  .3132935  .675   1.94591
5 2013 .00010507122 .024198003   .003155996  .2015814      .11627  3.403482e-09   .4159705   8.84072          0 1.0556953 .29048213    .5 2.1972246
end
label values id firm
label def firm 1 "000360206", modify
label def firm 2 "000361105", modify
label def firm 3 "000886309", modify
label def firm 4 "000957100", modify
label def firm 5 "00101J106", modify