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: nested loop for mean and median


From   Nahla Betelmal <[email protected]>
To   [email protected]
Subject   Re: st: nested loop for mean and median
Date   Mon, 29 Apr 2013 16:02:14 +0100

Thank you Maarten and Nick for the help and codes. And thanks Nick for
the detailed corrections. I appreciate it.

Thanks again

Nahla

On 29 April 2013 15:26, Nick Cox <[email protected]> wrote:
> Lots of problems here. Some arise from just not reading the documentation.
>
> 1. What is throwing you out is a type mismatch. At a guess, one or
> more of -fyear- -sic- -country- is actually a string variable. I can't
> tell you which: look at -describe-.
>
> 2. -summarize- does not calculate the median except with the -detail-
> option. When it does it saves it as r(p50). Why did you type
> r(median)?
>
> 3. For each variable, second time round the second loop
>
> gen mean_Assets = ...
>
> for example will fail because the variable already exists.
>
> Something like this is much easier:
>
> foreach v in  Assets Liab Income {
>         egen mean_`v' = mean(`v'), by(country year sic)
>         egen median_`v' = median(`v'), by(country year sic)
> }
>
> Nick
> [email protected]
>
>
> On 29 April 2013 15:15, Nahla Betelmal <[email protected]> wrote:
>> Dear Statalis,
>>
>> I need to get the mean and median for some firms in a certain year
>> (2000-2012) and certain industry (73 industries). e.g. the mean and
>> median for assets for the firms operate in industry 6 in year 2004,
>> and so on. I tried the following nested loop but I got error (109).
>>
>> p.s. I need the mean and median as new variables as they will be used
>> in further calculations.
>>
>>
>>
>>
>> foreach v of var  Assets Liab Income {
>>         forval y=2000/2012 {
>>                          forval  i= 1/73 {
>>                                 qui su `v' if  fyear==`y'& sic==`i'& country==1
>>                                 gen mean_`v' = r(mean)
>>                                 gen median_`v' = r(median)
>> }
>> }
>> }
>>
>> type mismatch
>> r(109);
>>
>>
>> Many thanks
>>
>> Nahla Betelmal
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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