Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Using gen / egen in one step (not two)


From   "Scott Merryman" <[email protected]>
To   <[email protected]>
Subject   st: RE: Using gen / egen in one step (not two)
Date   Sat, 6 Jan 2007 06:05:41 -0600

You could modify -_growmean- to include a multiplicative factor.  For
example:

*! version 1.0.0  05jan2007
*! Based on _growmean
program define _growmean2
	version 9, missing
	gettoken type 0 : 0
	gettoken h    0 : 0 
	gettoken eqs  0 : 0

	syntax varlist(min=1) [if] [in] [, BY(string) factor(real 1.0)]
	if `"`by'"' != "" {
		_egennoby rowmean() `"`by'"'
		/* NOTREACHED */
	}

	tempvar NOBS touse g
	mark `touse' `if' `in'
	quietly { 
		gen double `g' = 0 if `touse'
		gen long `NOBS' = 0 if `touse'
		tokenize `varlist'
		while "`1'"!="" {
			replace `g' = `g' + cond(`1'>=.,0,`1') if `touse'
			replace `NOBS' = `NOBS' + (`1'<.) if `touse'
			mac shift 
		}
		gen `type' `h' = (`g'/`NOBS')*`factor' if `touse'
	}
end

Scott


> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Jeremy Miles
> Sent: Friday, January 05, 2007 5:57 PM
> To: [email protected]
> Subject: st: Using gen / egen in one step (not two)
> 
> Hi All,
> 
> [Possibly foolish beginner question here.]
> 
> Am I missing something obvious here, when I try to combine two steps
> into one line:
> . sysuse auto
> . egen x = rowmean(price mpg) * 3
> 
> Stata says:
> varlist not allowed
> r(101);
> 
> For:
> . egen x = 3 * rowmean(price mpg)
> 
> Stata says:
> r(198);
> (Invalid syntax)
> 
> I can do it in two steps, but that seems inelegant.  Have I missed
> something obvious?
> 
> Thanks,
> 
> Jeremy
> 


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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