Stata creating output for IV regression with bysort
<p>So I am running an 2SLS model by interview year and I have many interview years and different models. I want to present the first-stage results first and then after reassuring the reader that they are solid move on to the interesting results. </p>

<p>Example of Table A (first stage):</p>

<p>Year DV   Coef SE   F    N</p>

<p>1    A     0.5 0.1 100 1000</p>

<p>2    A     0.8 0.2 10 1500</p>

<p>3    B    -0.6 0.4 800 800</p>

<p>Table B with the main results would look the same just without the F-Stat.</p>

<p>I searched on the web about how to create those tables automatically in Stata, but despite finding many questions I didn't find an answer that worked for me. From those different posts and help-files I build something that is nearly there.</p>

<p>It creates the table I want for the main results with the F-Stat together by some variable (Step A in the code). However, when I move on to do the same for the first stage it only saves the last wave as I restore the estimates. I understand why Stata does it like that, but I cannot think of a way of convincing it to do what I want.</p>

<pre><code>clear all

*Install user-written commands
ssc install outreg2, replace
ssc install ivreg210, replace

*load data
sysuse auto, clear

*run example model (obviously the model itself is bogus)
********************************************************
*Step A: creates the IV results by foreign plus the F-Statistic
bys foreign:  ///
    outreg2 using output1-IV-F, label excel stats(coef se) dec(2) adds(F-Test, e(widstat)) nocons nor2 keep(mpg) replace: ///
    ivreg210 price headroom trunk (mpg=rep78 ), savefirst first
*Step B: creates the first stage results in a seperate table
bys foreign:  ///
    ivreg210 price headroom trunk (mpg=rep78 ), savefirst first
    est restore _ivreg210_mpg   
    outreg2 using output1_1st-stage, replace keep(rep78)

cap erase output1-IV-F
cap erase output1_1st-stage
</code></pre>

<p>So ideally I would only run the model once and have the F-Stat in the first-stage table, but I can fix that manually. The biggest issue I have is how to store the estimates when using bysort. If anyone has any suggestions about that, I would greatly appreciate it.</p>

<p>Thanks!</p>