Bookmark and Share

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


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

Re: st: Calculating average value using STATA program language


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Calculating average value using STATA program language
Date   Mon, 12 Sep 2011 16:11:53 +0100

In your example, -exporter- probably means -importer-.

Otherwise, this is a very reasonable example of using a loop.

It is, however, not needed as

egen avg = mean(import), by(importer)

gets you there in one.

I don't know how you want to take account of -isic-, but you could do this too

egen avg = mean(import), by(importer isic)

Nick

On Mon, Sep 12, 2011 at 3:28 PM, Suryadipta Roy <[email protected]> wrote:
> Nick,
> Thank you! I am sure that Stata has other ways (like using -foreach-
> etc.) to arrive at the solution, and as Phil has suggested, probably
> it was a good exercise in the long run for me to check out the
> manuals. I am sure that I need to do lot more of that. I was actually
> trying something like the follwing:
>
> gen double avg=.
> qui levelsof importer, local(clist)
> foreach i of local clist {
> su import if importer=="`i'", meanonly
> qui replace avg=r(mean) if exporter=="`i'"
> }
>
>
> However, I understood that this is not going to work, since I did not
> know a way to incorporate the industry categories (isic) in the
> program. To sum it up, I clearly understand that I need to devote a
> lot more time on the manuals, and the related materials to understand
> Stata programming.
>
> Sincerely,
> Suryadipta.
>
> On Mon, Sep 12, 2011 at 4:21 AM, Nick Cox <[email protected]> wrote:
>> 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 <[email protected]> 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/


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