Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Outreg2 in a loop and table title

    Hello everyone,

    I am facing an issue with the outreg2 command. So I am trying to run a loop like this:
    local name3 "jcissue jc_sc jc_st jc_gen personwrk_allot hh_wrkd person_wrkd schh_wrkd sthh_wrkd persondgen_wrkd scpersond_wrkd stpersond_wrkd wpersond_wrkd persond labexp_disb matexp_disb labexp_dp matexp_dp"
    foreach name3 in jcissue jc_sc jc_st jc_gen personwrk_allot hh_wrkd ///
    person_wrkd schh_wrkd sthh_wrkd persondgen_wrkd scpersond_wrkd ///
    stpersond_wrkd wpersond_wrkd persond labexp_disb matexp_disb ///
    labexp_dp matexp_dp {
    bysort year: regress `name3' cpgender if year==2011, vce(cluster blockid)
    outreg2 using ols.xls, bdec(3) excel ctitle("`name3'", "2011", "Gender only") append
    }

    Basically what I would like is to is to have all the regression tables for each of the years compiled in an excel file. The problem is the table created by outreg2 is only showing table for the first year and not the subsequent ones. I can run regression for separate years but that will be time consuming. Could you please help?

    Another issue that I am facing is that I would like each of the year regressed on to appear as a column title or notes, whichever is possible. So far I have tried multiple commands but nothing is working out. Please advise me on this.

  • #2
    outreg2 is from SSC (FAQ Advice #12)


    bysort year: regress `name3' cpgender if year==2011, vce(cluster blockid)
    Here, you are asking that the regression be done by year
    bysort year
    and at the same time constraining the regression to the year 2011
    if year==2011
    The following will produce what you ask for:

    Code:
    webuse grunfeld, clear
    set seed 06182021
    forval i=1/5{
        gen var`i'= runiformint(1,`i'0)
    }
    *START HERE
    local i 0
    local name3 invest mvalue kstock
    levelsof year, local(years)
    foreach name of local name3{
        foreach year of local years{
            regress `name' var1 var2 var3 var4 var5 if year==`year', vce(cluster company)
            local opt= cond(`i'==0, "replace", "append")   
            outreg2 using myfile, `opt' ctitle("`name' `year'") excel
            local ++i
        }
    }
    Res.:

    Code:
     
    (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14) (15) (16) (17) (18) (19) (20) (21) (22) (23) (24) (25) (26) (27) (28) (29) (30) (31) (32) (33) (34) (35) (36) (37) (38) (39) (40) (41) (42) (43) (44) (45) (46) (47) (48) (49) (50) (51) (52) (53) (54) (55) (56) (57) (58) (59) (60)
    VARIABLES invest 1935 invest 1936 invest 1937 invest 1938 invest 1939 invest 1940 invest 1941 invest 1942 invest 1943 invest 1944 invest 1945 invest 1946 invest 1947 invest 1948 invest 1949 invest 1950 invest 1951 invest 1952 invest 1953 invest 1954 mvalue 1935 mvalue 1936 mvalue 1937 mvalue 1938 mvalue 1939 mvalue 1940 mvalue 1941 mvalue 1942 mvalue 1943 mvalue 1944 mvalue 1945 mvalue 1946 mvalue 1947 mvalue 1948 mvalue 1949 mvalue 1950 mvalue 1951 mvalue 1952 mvalue 1953 mvalue 1954 kstock 1935 kstock 1936 kstock 1937 kstock 1938 kstock 1939 kstock 1940 kstock 1941 kstock 1942 kstock 1943 kstock 1944 kstock 1945 kstock 1946 kstock 1947 kstock 1948 kstock 1949 kstock 1950 kstock 1951 kstock 1952 kstock 1953 kstock 1954
    var1 23.92* 31.16 -11.17 2.637 -7.928 2.262 -29.62* 15.90 5.973 -2.827 -24.59 10.48 -5.369 -13.32 5.664 8.186 43.29 21.66 29.14 62.31 214.4 351.9** 355.4 10.89 -349.9* -25.63 -134.0 88.16 -61.49 -88.00 -95.94 205.1 -38.57 -65.87 70.62 110.3 163.0 26.31 -19.59 220.9 -4.666 -3.490 5.949 9.596 -1.519 7.277 -17.62* 19.44 13.14*** -11.06 1.036 11.16 32.62 -74.46* -25.05 3.255 84.96** -10.69 -7.837 105.8
    (12.51) (24.65) (8.757) (2.130) (12.64) (25.71) (15.83) (17.11) (13.60) (20.81) (16.64) (39.89) (25.72) (17.44) (40.30) (19.58) (31.53) (38.44) (31.91) (67.03) (147.0) (123.4) (403.5) (46.65) (162.7) (239.3) (159.6) (145.0) (118.0) (172.0) (183.1) (213.6) (202.2) (78.82) (294.0) (108.3) (158.8) (232.5) (175.0) (264.1) (9.677) (9.961) (9.879) (14.18) (19.10) (9.740) (9.127) (24.72) (3.158) (22.22) (10.28) (31.26) (52.90) (36.67) (40.13) (39.37) (37.52) (103.0) (48.64) (85.50)
    var2 12.47 8.296 -23.71*** -12.11*** 3.723 10.18 -4.094 -17.07 -25.01* -0.876 23.13 -4.885 -6.620 0.130 16.94 -0.732 -14.20 9.685 14.83 30.60 52.62 33.06 -114.6 -83.18* 39.41 115.2 -39.82 -85.61 -192.7* 34.11 185.0 -88.48 -61.62 -73.79 69.12 -30.28 -85.12 -42.00 51.00 -57.94 -12.67** 7.479** 0.125 -1.593 -1.817 3.284 7.684 -2.142 -1.667 4.900 6.728 -25.95 9.756 -20.93 32.97** 7.255 27.76 -5.746 -1.889 47.25
    (7.515) (5.487) (6.618) (1.261) (3.906) (16.86) (4.076) (10.67) (12.45) (10.05) (13.26) (32.26) (15.38) (9.141) (10.21) (10.17) (15.33) (14.91) (19.92) (47.13) (87.01) (37.68) (186.7) (43.15) (50.81) (165.5) (40.07) (85.86) (102.2) (78.99) (124.4) (195.9) (91.34) (47.89) (66.73) (49.12) (95.28) (93.82) (81.30) (168.1) (4.037) (3.119) (3.377) (5.611) (5.845) (9.577) (6.830) (7.765) (4.106) (17.12) (6.559) (24.18) (33.57) (16.34) (12.20) (19.82) (22.27) (35.97) (28.25) (58.34)
    var3 6.173 -0.00448 2.238 -6.868*** 0.0652 10.25 2.850 -3.912 -4.011 -2.394 -5.529 11.62 -17.88** -3.632 9.566 -11.44 2.288 -10.46 -37.81* 9.558 55.71 27.43 59.40 -94.45*** 11.13 84.85 -5.317 -35.31 -60.70 -54.70 -54.74 80.89 -91.25 -10.94 73.88 -57.74 47.85 -46.67 -131.2 43.70 -8.641** 3.052 4.600** -6.094 -2.269 -6.657 -4.042 -4.125 1.437 7.448 11.39** -12.56 -5.773 -22.14 23.85*** -19.01 7.667 -3.348 -30.10 30.67
    (7.753) (5.299) (2.782) (0.893) (2.787) (11.38) (11.05) (7.855) (5.445) (7.946) (6.162) (16.90) (7.845) (7.887) (6.956) (6.860) (13.71) (8.517) (19.02) (17.75) (87.28) (36.96) (82.27) (29.04) (35.66) (109.5) (112.7) (65.68) (47.89) (58.86) (58.15) (107.6) (56.35) (36.73) (46.32) (33.75) (77.87) (49.65) (95.90) (65.98) (3.251) (3.459) (1.770) (3.809) (4.131) (5.646) (6.598) (6.552) (2.262) (9.912) (4.919) (14.16) (14.94) (12.73) (4.212) (11.97) (16.88) (23.72) (27.88) (22.82)
    var4 -4.116 -6.565 -8.949** 2.478** -5.618** 2.653 -2.471 -0.851 3.702 5.766 -4.463 -9.809 2.007 9.134 -2.011 -4.145 -4.459 13.87 -10.62 4.793 -48.18 -61.04 -114.0* -18.76 -85.24** -12.59 -31.50 -18.77 26.64 37.00 -33.34 -29.27 -2.435 44.79 -9.513 -11.83 -13.62 65.22 -49.43 71.44 1.825 -2.875 -5.429*** 0.950 -6.641** 0.234 -4.762 -9.717 8.722*** 6.268 -3.818 1.420 -3.656 -14.21 -1.658 2.128 -6.700 9.475 -14.57 -4.394
    (6.135) (5.974) (2.764) (0.807) (2.435) (4.313) (3.682) (6.827) (3.786) (5.198) (5.816) (14.47) (4.581) (7.244) (4.501) (4.712) (7.115) (10.76) (12.80) (22.95) (68.55) (52.27) (58.44) (25.25) (30.49) (42.50) (37.97) (62.45) (33.66) (41.63) (56.39) (85.76) (32.55) (37.03) (24.21) (17.35) (37.34) (75.76) (63.84) (88.88) (2.562) (3.658) (1.167) (2.014) (2.454) (2.179) (3.537) (6.200) (1.066) (5.049) (2.563) (11.25) (9.911) (13.36) (6.308) (6.752) (7.605) (30.71) (14.79) (29.14)
    var5 -7.044*** 0.815 3.951** 3.510*** 3.080 3.079 -9.737* 7.249 0.891 -5.511 2.197 6.252 -2.124 -5.935*** -4.021 -5.972 7.102 12.36 2.996 -1.878 -39.12 -2.247 86.56 29.89 -8.555 3.594 -76.54 36.81 24.12 -43.16 7.068 51.47 -8.018 -23.65*** -24.85 -38.20* 27.44 43.76 15.31 -6.119 -2.686 0.455 -2.048 4.441 3.036 5.458** -2.866 1.997 1.133 5.180 -7.219** -1.428 -3.326 7.588 -3.494 -2.160 9.885** 11.72 -8.021 -2.165
    (1.864) (3.791) (1.475) (0.639) (2.330) (3.306) (4.635) (3.984) (3.420) (5.724) (2.066) (4.607) (3.788) (1.433) (4.480) (3.750) (5.153) (9.103) (9.562) (15.56) (21.79) (35.65) (48.24) (20.19) (23.73) (33.14) (48.87) (30.94) (24.70) (43.82) (26.75) (28.26) (31.99) (6.062) (23.88) (17.49) (20.06) (59.81) (45.75) (61.79) (1.799) (2.067) (1.190) (2.951) (2.533) (1.989) (3.075) (4.481) (1.439) (5.346) (2.784) (4.548) (7.564) (5.054) (3.259) (6.901) (3.729) (20.98) (13.06) (20.41)
    Constant 62.38 7.440 476.3** 107.3*** 92.85 -283.3 640.5 145.4 356.6 220.4 112.7 78.56 589.9* 192.0 -33.53 526.2*** -10.40 -542.7 791.6 -610.0 655.0 268.8 -374.3 2,414** 4,057** -1,054 5,070 1,446 3,695* 2,428 987.6 -487.6 3,556 1,622 -201.6 2,678** 24.39 -584.9 4,142 -773.8 408.1*** 36.93 153.7 9.197 253.5 62.35 403.9* 311.7 -75.53 -195.7 227.4 757.8 302.3 1,563*** -1.752 610.8 -601.2 70.56 1,734* -822.7
    (111.0) (168.3) (147.5) (32.20) (148.1) (211.6) (381.7) (259.1) (219.1) (256.8) (87.39) (361.1) (261.8) (294.0) (196.2) (159.9) (279.1) (499.4) (525.4) (1,022) (1,253) (1,661) (5,398) (794.5) (1,693) (2,031) (3,778) (2,258) (1,799) (1,983) (705.0) (2,209) (2,059) (1,298) (1,221) (885.4) (1,685) (2,809) (2,424) (3,820) (66.43) (84.29) (119.2) (163.0) (202.0) (141.4) (217.4) (300.2) (90.02) (369.9) (132.7) (555.9) (568.7) (454.0) (142.1) (447.6) (427.4) (1,008) (796.7) (1,274)
    Observations 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
    R-squared 0.533 0.415 0.908 0.981 0.794 0.378 0.769 0.711 0.640 0.414 0.515 0.427 0.746 0.690 0.553 0.550 0.410 0.603 0.659 0.406 0.302 0.302 0.622 0.818 0.766 0.270 0.609 0.490 0.585 0.434 0.382 0.565 0.582 0.707 0.580 0.638 0.380 0.438 0.584 0.393 0.810 0.599 0.939 0.648 0.661 0.727 0.545 0.359 0.921 0.356 0.787 0.390 0.381 0.582 0.853 0.448 0.490 0.264 0.578 0.499
    Robust standard errors in parentheses

    Comment


    • #3
      Hi Andrew, thank you so much for your help and pointing out the error. My original code didn't have the "if year" part. But I deleted it from my do file and I copy pasted and edited another code here and forgot to remove the "if year" part. I will edit my post if I can.

      Comment


      • #4
        Hello Andrew, the codes that you mentioned worked! Thanks again. But I have one quick question. I read somewhere in this forum that STATA allows foreach allows only one local. But in this code, we use two locals and two foreach. Does STATA allow this because the loop is nested and for each foreach command, there is a separate local? Or did we just circumvented STATA rules, if that's possible at all? I am just trying to understand the logic.

        Comment


        • #5
          I read somewhere in this forum that STATA allows foreach allows only one local.
          Not so, you can reference several locals. Probably, you misunderstood the context.

          Code:
          local colors red orange yellow
          local directions north east west
          local birds eagle hawk robin
          foreach element in `colors' `directions' `birds'{
              di "`element'"
          }
          Res.:

          Code:
          . local colors red orange yellow
          
          .
          . local directions north east west
          
          .
          . local birds eagle hawk robin
          
          .
          . foreach element in `colors' `directions' `birds'{
            2.
          .     di "`element'"
            3.
          . }
          red
          orange
          yellow
          north
          east
          west
          eagle
          hawk
          robin
          
          .

          On the spelling of Stata, see https://www.statalist.org/forums/help#spelling.

          Comment


          • #6
            Thanks a lot for explaining me Andrew. Actually, I read it in this post: https://www.statalist.org/forums/for...-the-same-time

            But now I understand that the codes and contexts are different.
            Last edited by Anwesha Bandyopadhyay; 19 Jun 2021, 04:22.

            Comment

            Working...
            X