Statalist


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

Re: st: AW: Using svy with by


From   Austin Nichols <austinnichols@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: AW: Using svy with by
Date   Tue, 4 Aug 2009 08:45:45 -0400

ssc install _gwtmean
sysuse auto
egen m=wtmean(price), by(rep78) weight(weight)

On Tue, Aug 4, 2009 at 7:16 AM, Martin Weiss<martin.weiss1@gmx.de> wrote:
>
> <>
>
> Apart from other problems, you are treating -mean- as if it was a function.
> You have to -generate- the mean estimate first, and then do the -replace-
> separately...
>
> Note the difference:
>
> *************
> sysuse auto, clear
> by f: gen mean=mean(pr)
> *************
>
> *************
> sysuse auto, clear
> gen meanpr=.
> levelsof for, loc(myfor)
>
> foreach lev in `myfor'{
>        su pr if for==`lev', mean
>        replace meanpr=`r(mean)' if for==`lev'
> }
> *************
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von
> A.Dijkstra@lse.ac.uk
> Gesendet: Dienstag, 4. August 2009 12:14
> An: statalist@hsphsun2.harvard.edu
> Betreff: st: Using svy with by
>
> Hi all,
>
> I'm trying to find a way around a problem. The code below results in an
> error since svy cannot by combined with by. I cannot think of another way to
> get the equivalent result I need, however, since the mean I'm attempting to
> calculate must be run using svy.
>
> Any ideas?
>
> Code:
>
> foreach varname of varlist `PAYMENTVARS' {
> generate `varname'PANAVG = 0
>  by DUPERSID PURCHRD, sort: svy: replace `varname'PANAVG = mean(`varname')
>  by DUPERSID PURCHRD: keep if _n==_N
>  drop `varname'
>  }
>
>
> Error:
>
> svy may not be combined with by
> r(190);
>
>
>
> Regards,
>
> A
>

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index