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 qing ye 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 06:34:10 +0800

```hi,

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?

Best
Qing

On 8 December 2011 03:17, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> We can only respond to what you explain.
>
> In principle -collapse- can be extended to much more complicated problems. You need to try it, not to speculate. Did you try my code?
>
> Once you have a dataset of summary statistics, the problem of tabulation largely disappears. Again, if you start looking at the documentation carefully and apply the commands, this will become clearer.
>
> Nick
> n.j.cox@durham.ac.uk
>
> qing ye
>
> Hi, Nick
>
> Thanks for the information.
>
> Actually, I will want to calculate several statsitics using that loop.
> in addition, I will want to based on the group+year, calculate some
> statistics for the group-year over the next few periods.  for example,
> I have a group in 1990, then I want to calculate some statistcs for
> stocks in the group over the next 12 months, so the code I provided
>
> I guess the collapse will not work if I want to include all the
> statistics calculated for each group at each period  in a panel-format
> table?
>
> So how do I exactly can store the statistics I calculated in a loop
> into one cell of a table?  I used to do this kind of analysis in VB,
> which is strictforward, after each loop, I store the value into one
> cell of an arrany, and I have no idea how do I do that in stata, not
> even a clue on what kind of commends I should use.
>
> Best
> Qing
>
> On 8 December 2011 02:39, Nick Cox <n.j.cox@durham.ac.uk> wrote:
>> -collapse- in particular should mean that you do not need to loop at all.
>>
>> You can calculate all the ratios of sums by a single -generate-.
>>
>> collapse (sum) s_EI = EI_t (sum) s_BE = BE_t_1 if inrange(trade_date_yearformat, 1990, 2010), by(FF_group_rank_0 trade_date_yearformat)
>>
>> gen mean_EI_BE = s_EI / s_BE
>>
>> Nick
>> n.j.cox@durham.ac.uk
>>
>> qing ye
>>
>> hi, I have checked collapse and tabstat, still no ideas about how to
>> use it combined with a loop?
>>
>> In the loop, everytime, I calculated one mean_EI_BE, how do I write
>> this one result into one cell of a table where the columns and rows of
>> the table reprsents different steps in a looping?
>>
>> On 8 December 2011 02:08, qing ye <yeqing1013@gmail.com> wrote:
>>
>>> Thanks for the quick reply.
>>>
>>> I will  check the help file for collapse and tabstat
>>>
>>> the group takes the value of 1-6, and the varible FF_group_rank_0
>>> takes values of 1-6 stating six groups, so I want to loop for each
>>> group, I guess I need to use forvalues rather than foreach?
>>
>> On 8 December 2011 02:00, Nick Cox <n.j.cox@durham.ac.uk> wrote:
>>
>>>> I'd look at -collapse- and/or -tabstat-.
>>>>
>>>> In any case, your loop
>>>>
>>>>      foreach group of varlist FF_group_rank_0 {
>>>>
>>>>        }
>>>>
>>>> will do nothing as `group' is never referred to inside the loop. That probably doesn't matter, but it shows some confusion about what -foreach- does.
>>
>> qing ye
>>
>>>> Do any of you know how to report the results calculated after the loop
>>>> as tables??
>>>>
>>>> forvalues i=1990 (1)2010 {
>>>>      foreach group of varlist FF_group_rank_0 {
>>>>              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'
>>>>
>>>>
>>>>              }
>>>>              }
>>>>              }
>>>>
>>>> I wanted to have the mean_EI_BE I calculated in the loops reported as a table
>>>>
>>>> something like the following:
>>>>
>>>> year      group1 group2 group3 group4 group5 group6
>>>>
>>>> 1990       x           x        x
>>>> 1991       x           x        x
>>>> 1992       x           x        x
>>>> 1993       x           x        x
>>>> ....
>>>>
>>>>
>>>>
>>>> How do I do this? Do I need to use matrix commends to save the
>>>> mean_EI_BE I calculated each time or do I need to generate some
>>>> variables before the loop and store the mean_EI_BE in those variables?
>>
>> *
>> *   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/
>
> *
> *   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/
```