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

# Re: st: Calculating average value using STATA program language

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Calculating average value using STATA program language Date Mon, 12 Sep 2011 09:21:21 +0100

```Suryadipta got excellent advice to look at -collapse- and the problem
appears to be solved. However, if the main problem really had been
misunderstanding of loops, it would have been hard to advise on the
precise misunderstanding, as no Stata commands were included in this
post. Saying exactly what you typed and what then happened is another
key piece of advice.

Nick

On Mon, Sep 12, 2011 at 4:47 AM, Suryadipta Roy <sroy2138@gmail.com> wrote:

> I am a newbie in terms of writing Stata codes, and intend to get that
> corrected pretty soon. Meanwhile, I was wondering if any one could
> help me with the following. I have been trying use foreach/forvalues
> without any success for some time. Any help/suggestion/hint is greatly
> appreciated.
>
> importer        exporter        year    isic     imports   sum_import   avg_import
> AUS     FRA     1979    311     8       24      12
> AUS     FRA     1980    311     16      24      12
> AUS     FRA     1979    313     0       0       0
> AUS     FRA     1980    313     0       0       0
> AUS     GAB     1979    369     0       0       0
> AUS     GBR     1976    311     10      30      10
> AUS     GBR     1977    311     20      30      10
> AUS     GBR     1978    311     0       30      10
> AUT     CAN     1977    312     10      10      10
> AUT     CAN     1978    390     20      20      20
> FRA     AUS     1976    311     5       10      5
> FRA     AUS     1977    311     5       10      5
> FRA     AUT     1980    369     4        4      4
>
> In the hypothetical dataset above, I am trying to create the last two
> columns (sum_import, and avg_import) from the "imports" column. For
> the first column, the idea is to calculate the sum of imports e.g.
> from FRA to AUS for each industry category (isic). Thus for the isic
> category 311, the sum of imports in 1979 and 1980 is 8 + 16 = 24, and
> the average in the two years is 12. Similarly, there were no trade
> between AUS and GAB in 1979 for the industry category 369, and hence
> the sum and the average value of imports = 0. The actual dataset has
> over 200 countries covering data from 1976 - 1980 and I am trying to
> calculate the average of the imports between 1976- 1980 in each
> industry category. There are 28 such industry categories.
>

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