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

# Re: st: RE: how to store the statistics calculated in a loop as a table?

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: RE: how to store the statistics calculated in a loop as a table? Date Thu, 8 Dec 2011 10:09:07 +0000

```In addition to Kit's example, please study this:

sysuse auto, clear
collapse (sum) w_sum = weight (sum) w_length = length, by(foreign rep78)
gen ratio = w_sum / w_length
tabdisp rep78 foreign, cell(w_sum w_length ratio)

There are no loops!

Nick

On Thu, Dec 8, 2011 at 2:48 AM, Christopher Baum <kit.baum@bc.edu> wrote:

> I just tried the collapse, it is really handy, it is amazing how stata
> create so powerful commend. I guess my old way of programming in VB is
> very narrow and restricted.
>
> But I still wanted to know if there is anyway that I could store the
> results I calculated in the loop as a table? Do I need to use matrix
> related commend?
>
> The following is the code:
>
> forvalues i=1990 (1)2010 {
>                forvalues n=1(1)6{
>
>                             quietly sum  EI_t if
>                             local s_EI=r(sum)
>                             quietly sum  BE_t_1 if
>                             local s_BE=r(sum)
>                             local mean_EI_BE=`s_EI'/`s_BE'
>
>             }
>             }
>
>
> How do I make stata to memorize the mean_EI_BE value that is
> calculated after each loop , and at then end report all the mean_EI_BE
> as a seperate table?
>
>
> -------------------------------
> webuse grunfeld, clear
> egen quinq = cut(year), group(4) label
> egen quincomp = group(quinq company), label
> // quinq are time periods (quinquennia), company are units in this panel
> tabstat invest kstock, by(quincomp) save
> mata:
> m = J(0,2,0)
> for(i=1; i<=40; i++) {
>        m = m \ st_matrix("r(Stat"+strofreal(i)+")")
> }
> m = m , m[. , 1] :/ m[. , 2]
> st_matrix("Result", m)
> end
> mat li Result
> ---------------------------------
> You can add the row labels (which are also saved by tabstat) if you wish.
>

*
*   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/
```