Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# st: RE: formatting looping regression results

 From "Pagel, Christina" To "statalist@hsphsun2.harvard.edu" Subject st: RE: formatting looping regression results Date Thu, 13 Sep 2012 14:23:35 +0000

```Hi,

I do something similar by creating local matrices within my DO file, writing out the results of individual regressions to these matrices and finally displaying the matrix in stata. I can then easily copy and paste this into Excel (which is where I like to pretty my tables up - I know tabout is good in Stata but I've never found the term to learn it)...

Here is some example code:

*set a temporary name for the results table output by STATA
tempname TmpTable

matrix Results = J(4,9,.)  // matrix to store results of mean commands below
matrix colnames Results="Mean 1" "Mean 2" "Mean 3" "LB 1" "LB 2" "LB 3" "UB 1"  "UB 2" "UB 3"
matrix rownames Results= "Var 1" "Var 2" "Var 3" "Var 4"
local RowNum = 0 // pointer to matrix row
local ColNumMean // pointer to matrix column for mean
local ColNumLB // pointer to matrix column for lower bound of confidence interval
local ColNumUB // pointer to matrix column for upper bound of confidence interval

foreach CountVar of varlist Var1 Var2 Var3 Var4 {	//loop for variables
*increment row
local RowNum=1+`RowNum'

foreach DelType of numlist 0 1 2 {	//loop for mean types (DelType)

*this is how I have my table set up... (see col names for matrix above)
local ColNumMean=`DelType'+1
local ColNumLB=(`DelType'*2)+4
local ColNumUB=(`DelType'*2)+5

*this is where I do the actual regression
quietly xtreg `CountVar' if TrialArm==0 & DeliveryType==`DelType', i(TrialCluster) re

*r(table) is a very useful stata command that can be used to output all results into a matrix
matrix `TmpTable'=r(table)

/* matrix looks like this - for your regression command have a look at what r(table) returns!
_cons
b  .49740385
se   .0793922
z  6.2651476
pvalue  3.725e-10
ll    .341798
ul   .6530097
df          .
crit   1.959964
eform          0

I want elements [1,1], [5,1] and [6,1] */

matrix Results[`RowNum',`ColNumMean'] = `TmpTable'[1,1]
matrix Results[`RowNum',`ColNumLB'] = `TmpTable'[5,1]
matrix Results[`RowNum',`ColNumUB'] = `TmpTable'[6,1]
}
}

*finally I display the matrix with headers to check it looks ok
matrix list Results

*to make it easier to copy and paste I display again without headers

Hope this helps!
Christina
-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Israel Pearce
Sent: 13 September 2012 12:28 AM
To: statalist@hsphsun2.harvard.edu
Subject: st: formatting looping regression results

Hello,

I am running regressions in a loop and want to find a good way to get output to excel. I can't seem to work eststo and estout quite right.
Here is the idea:

Looping through an id# and 8 LHS varibles (RHS variables are the same) leaves me with 8 regressions per id from which I want a b, se, r2, N.

Ideally, I want to save them as
id#1
b1 (reg1) b1(reg2) b1(reg3)................
se (reg1) se(reg2)..................
b2 (reg1).................................
se2 (reg1)...................
r2(reg1)
N(reg1)

id#2
b1(reg1)
se(reg1) etc....

In general would eststo and estout work here? I am having trouble creating an identifier for each regression using that, and then understanding how to stack it how I want to. Thanks!
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```