Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: nested loop for mean and median


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: nested loop for mean and median
Date   Mon, 29 Apr 2013 16:19:43 +0200

Don't use loops for that, just type:
bys sic fyear country: egen mean_assests = mean(assets)
bys sic fyear country: egen median_assests = median(assets)

Hope this helps,
Maarten


On Mon, Apr 29, 2013 at 4:15 PM, Nahla Betelmal <nahlaib@gmail.com> 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/



-- 
---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index