# Re: st: Averages by groups but with one specified observation

 From Sergiy Radyakin To statalist@hsphsun2.harvard.edu Subject Re: st: Averages by groups but with one specified observation Date Fri, 13 Nov 2009 16:13:19 -0500

```Hi, Jennifer,

try the code below (not particularly optimized, but seems to work).

clear all

program define strangemean, sortpreserve

syntax varname(numeric), by(varname) generate(name) w(varname numeric)
sort `by'
tempvar PW NUMER DENOM
local y `"`varlist'"'

if missing(`"`w'"') {
tempvar w
generate byte `w'=1
}

by `by': generate `PW'=`y'*`w'
egen `NUMER'=sum(`PW'), by(`by')
egen `DENOM'=sum(`w'), by(`by')

generate `generate'=.
replace `generate'=(`NUMER'-`y'*`w')/(`DENOM'-`w')
end

sysuse auto
sort rep78 // this is just for the listing
strangemean price, by(rep78) generate(strange_mean) w(weight)
format strange_mean %8.0gc
list rep78 weight price strange_mean, sepby(rep78) ab(16)

*******************
Output
*******************

+----------------------------------------+
| rep78   weight    price   strange_mean |
|----------------------------------------|
1. |     1    2,730    4,195          4,934 |
2. |     1    3,470    4,934          4,195 |
|----------------------------------------|
3. |     2    2,750    3,667        6,522.9 |
4. |     2    3,220    5,104        6,383.8 |
5. |     2    3,740    6,342        6,212.1 |
6. |     2    3,600    4,010        6,574.3 |
7. |     2    3,900   14,500        4,823.6 |
8. |     2    2,690    4,172        6,459.6 |
9. |     2    3,600    5,886        6,283.5 |
10. |     2    3,330    4,060        6,537.7 |

On Fri, Nov 13, 2009 at 12:27 PM, Solorzano Mosquera, Jenniffer
> Hi everyone
>
> I need to construct an average for each variable by different strata for each observation of data but without including the value added by that specific observation in the computed average. Any ideas?
>
> foreach x of varlist col nght dog cancel home atm {
> * Count cell size for each weight of each variable
>
>        qui egen strat1=count(`x')  if strata1~=. , by(strata1)
>        qui egen strat2=count(`x')  if strata2~=. , by(strata2)
>        qui egen strat3=count(`x')  if strata3~=. , by(strata3)
>        qui egen strat4=count(`x')  if strata4~=. , by(strata4)
>
> *******************
> * SIMPLE AVERAGES
> *******************
>
>        qui egen avge2=mean(`x') if strata2~=. , by(strata2) /* sample average */
>        qui egen avge3=mean(`x') if strata3~=. , by(strata3) /* sample average */
>        qui egen avge4=mean(`x') if strata4~=. , by(strata4) /* sample average */
>
>        qui egen Ia`x'=mean(`x') if strata1~=. , by(strata1) /* population and sample average by strata */
>        qui replace Ia`x'=avge2 if (strat1<10 | strat1==.) & strat2>=10 & strat2~=.
>        qui replace Ia`x'=avge3 if (strat2<10 | strat2==.) & strat3>=10 & strat3~=.
>        qui replace Ia`x'=avge4 if (strat3<10 | strat3==.) & strat4>=10 & strat4~=.
>        qui replace Ia`x'=. if (strat3<10 & strat4<10) & empresa!=`j'
>        qui label var Ia`x' "`x' - simple means"
>
>
> }
>
> Jenniffer Solorzano Mosquera
> Labor Markets Division - Inter-American Development Bank
> Washington, DC - USA
> (202) 623-3659
>
>
> *
> *   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/
```