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

Re: AW: st: AW: Using svy with by

From   Nick Cox <>
Subject   Re: AW: st: AW: Using svy with by
Date   Tue, 04 Aug 2009 08:39:58 -0500

_gwtmean is a package from SSC by David Kantor. It is documented briefly (and sufficiently) in the package description

. ssc desc _gwtmean

and at greater length within the code

. ssc type _gwtmean.ado

Incidentally, Martin's code

. by f: gen mean=mean(pr)

contains a typo. Use -egen-, not -gen-.


Martin Weiss wrote:

Is this thing documented anywhere? Does not seem to supply a help file...

[] Im Auftrag von Austin Nichols

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

On Tue, Aug 4, 2009 at 7:16 AM, Martin Weiss<> wrote:

Apart from other problems, you are treating -mean- as if it was a
You have to -generate- the mean estimate first, and then do the -replace-

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'

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
get the equivalent result I need, however, since the mean I'm attempting
calculate must be run using svy.

Any ideas?


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'


svy may not be combined with by

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index