Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Looping and saving regression outputs

From   Ada Ma <>
Subject   Re: st: Looping and saving regression outputs
Date   Fri, 18 May 2012 12:28:49 +0100

I don't know if this is any use to the OP.  But here is a double loop
I wrote to do detailed sum stats - I had to loop through both years
and indicators (40+ of them?) - there must be saner way of doing this
but I'm old fashioned...

* First you need an empty matrix - count the number of elements you
are going to capture
* this is the big matrix that you're going to -svmat- and -outsheet-
matrix mpatientratios = (.,.,.,.,.,.,.,.,.,.,.,.)

foreach year in 200506 200607 200708 200809 200910 {

* Note that I stick this local in to label the big matrix in case I got confused
local i=1
       foreach ind in $indlist {
       di "Indicators is `ind', `year'"
       sum mpatientearnings if year==`year' & ind=="`ind'" [fw=denom], det

* these locals capture the various bits that I am going to stick into
one line of matrix
* there must be smarter ways... I tried stick the r's directly into
matrix and it didn't work
       local h1=r(N)
       local h2=r(mean)
       local h3=r(sd)
       local h4=r(min)
       local h5=r(max)
       local h6=r(max)/r(min)
       local h7=r(p99)/r(p1)
       local h8=r(p95)/r(p5)
       local h9=r(p90)/r(p10)
       local h10=r(p75)/r(p25)

* this line append the new results to the big matrix
       matrix mpatientratios = mpatientratios \ (`i',`year',`h1' ,`h2'
,`h3' ,`h4' ,`h5',`h6' ,`h7' ,`h8' ,`h9' ,`h10')
       local i = `i'+1

Hope this helps.
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index