Hello everyone,

Firstly, I am a Stata 14 user. I have a question regarding esttab command and flipping models. My question might be considered as an extension of the question below,

https://www.statalist.org/forums/for...-nnmatch-coefs

I use three different specifications in the form of xtreg and xtivreg2 (i am using two different instruments). There are approximately 30 independent variables that I ran xtreg and xtivreg2. Hence, I have three column of results for a given independent variable. I want to place dependent variables in rows. When I use the code below, which I got from the above post, I can successfully get the coefficients and standard errors for my estimations in the format that I want to have. However, I want my table to include number of observation and number of cluster columns as well. Hence, I need a column of number of observations after the OLS (xtreg) column. Similarly, I need a column of number of observations and a column of number of clusters for the two xtivreg2 columns. My priority is to get a number of observations column after three different specification.

Code:
preserve 
eststo clear
eststo Subsidiesandothertransfers: xtreg Subsidiesandothertransfers distinctparty  if pr==1, fe cluster(idn)
eststo gtr_centaxdir: xtreg gtr_centaxdir distinctparty  if pr==1, fe cluster(idn)
esttab, noconstant se nostar r2
matrix C = r(coefs)
matrix S = r(stats)
eststo clear
local rnames2 : rownames C
local rnames
foreach name of local rnames2 {
    local rnames `rnames' `=strtoname("`name'")'
}
cap mat rownames C= "`rnames'"
local models : coleq C
local models : list uniq models
local i 0
foreach name of local rnames {
    local ++i
    local j 0
    capture matrix drop b
    capture matrix drop se
    foreach model of local models {
        local ++j
        matrix tmp = C[`i', 2*`j'-1]
        if tmp[1,1]<. {
            matrix colnames tmp = `model'
            matrix b = nullmat(b), tmp
            matrix tmp[1,1] = C[`i', 2*`j']
            matrix se = nullmat(se), tmp
        }
    }
    ereturn post b
    quietly estadd matrix se
    eststo `name'
}
local rnames2 : rownames S
local rnames
foreach name of local rnames2 {
    local rnames `rnames' `=strtoname("`name'")'
}
cap mat rownames S= "`rnames'"

local i 0
foreach name of local snames {
    local ++i
    local j 0
    capture matrix drop b
    foreach model of local models {
        local ++j
        matrix tmp = S[`i', `j']
        matrix colnames tmp = `model'
        matrix b = nullmat(b), tmp
    }
    ereturn post b
    eststo `name'
}
esttab, se mtitle noobs compress nonumb
est save res1, replace
restore, preserve
eststo clear

eststo Subsidiesandothertransfers: xtivreg2 Subsidiesandothertransfers (distinctparty =  gol_adm)  if pr==1, fe cluster(idn)
eststo gtr_centaxdir: xtivreg2 gtr_centaxdir (distinctparty =  gol_adm)  if pr==1, fe cluster(idn)
esttab, se nostar r2
matrix C = r(coefs)
matrix S = r(stats)
eststo clear
local rnames2 : rownames C
local rnames
foreach name of local rnames2 {
    local rnames `rnames' `=strtoname("`name'")'
}
cap mat rownames C= "`rnames'"
local models : coleq C
local models : list uniq models
local i 0
foreach name of local rnames {
    local ++i
    local j 0
    capture matrix drop b
    capture matrix drop se
    foreach model of local models {
        local ++j
        matrix tmp = C[`i', 2*`j'-1]
        if tmp[1,1]<. {
            matrix colnames tmp = `model'
            matrix b = nullmat(b), tmp
            matrix tmp[1,1] = C[`i', 2*`j']
            matrix se = nullmat(se), tmp
        }
    }
    ereturn post b
    quietly estadd matrix se
    eststo `name'
}
local rnames2 : rownames S
local rnames
foreach name of local rnames2 {
    local rnames `rnames' `=strtoname("`name'")'
}
cap mat rownames S= "`rnames'"

local i 0
foreach name of local snames {
    local ++i
    local j 0
    capture matrix drop b
    foreach model of local models {
        local ++j
        matrix tmp = S[`i', `j']
        matrix colnames tmp = `model'
        matrix b = nullmat(b), tmp
    }
    ereturn post b
    eststo `name'
}
esttab, se mtitle noobs compress nonumb
estimates save res2, replace
eststo clear

restore, preserve
eststo clear
eststo Subsidiesandothertransfers: xtivreg2 Subsidiesandothertransfers (distinctparty =  seatlvoter )  if pr==1, fe cluster(idn)
eststo gtr_centaxdir: xtivreg2 gtr_centaxdir (distinctparty =  seatlvoter )  if pr==1, fe cluster(idn)
esttab, se nostar r2
matrix C = r(coefs)
matrix S = r(stats)
eststo clear
local rnames2 : rownames C
local rnames
foreach name of local rnames2 {
    local rnames `rnames' `=strtoname("`name'")'
}
cap mat rownames C= "`rnames'"
local models : coleq C
local models : list uniq models
local i 0
foreach name of local rnames {
    local ++i
    local j 0
    capture matrix drop b
    capture matrix drop se
    foreach model of local models {
        local ++j
        matrix tmp = C[`i', 2*`j'-1]
        if tmp[1,1]<. {
            matrix colnames tmp = `model'
            matrix b = nullmat(b), tmp
            matrix tmp[1,1] = C[`i', 2*`j']
            matrix se = nullmat(se), tmp
        }
    }
    ereturn post b
    quietly estadd matrix se
    eststo `name'
}
local rnames2 : rownames S
local rnames
foreach name of local rnames2 {
    local rnames `rnames' `=strtoname("`name'")'
}
cap mat rownames S= "`rnames'"

local i 0
foreach name of local snames {
    local ++i
    local j 0
    capture matrix drop b
    foreach model of local models {
        local ++j
        matrix tmp = S[`i', `j']
        matrix colnames tmp = `model'
        matrix b = nullmat(b), tmp
    }
    ereturn post b
    eststo `name'
}

esttab, se mtitle noobs compress nonumb
est save res3, replace

estimates use res1
eststo res1
estimates use res2
eststo res2
estimates use res3
eststo res3
esttab res1 res2 res3, mtitles("OLS" "IV1" "IV2" ) nonumb noobs label

Thanks in advance.

Sincerely,
Matt