Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Egen and weights - passing values to egen functions

From   [email protected]
To   [email protected]
Subject   st: Egen and weights - passing values to egen functions
Date   Mon, 26 Aug 2002 14:30:05 -0400


I was working on getting egen - mean to generate weighted means. As far as I
know, there is no egen extension to do that, but I may be wrong. I modified
slightly Stata's _gmean function, and it works fine for my data. The only
problem is that in my function the name of your weight variable is not
important... unless the variable is called weight! I don't know how to pass
options from egen to the functions. Here is the rudimentary code, if nothing's
wrong with it (missing values, etc.) it may be easily transformed into a real
extension of egen, just by finding the right way to "pass" the weight (if people
find it is worthwhile, of course).


*! Stata's _gmean version 3.1.0  30jun1998
*! Modified: _gwmean: ... 26aug2002
program define _gwmean
     version 6
     syntax newvarname =/exp [if] [in] [, BY(varlist)]
     tempvar touse
     quietly {
          gen byte `touse'=1 `if' `in'
          sort `touse' `by'
          tempvar value

***Only difference with stata's _gmean
          gen `value'=`exp'*weight if `touse'==1
          by `touse' `by': gen `typlist' `varlist' = /*
               */ sum(`value')/sum(weight) if `touse'==1
***The variable weight should be `weight', or $weight, etc.

          by `touse' `by': replace `varlist' = `varlist'[_N]


Guillermo Cruces

PS: BTW, thanks for the comments on the weights/poverty. There is in fact an ado
file called sepov (STB-51 sg115) that calculates the survey standard errors.

*   For searches and help try:

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