Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

RE: st: replacing with mean


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: replacing with mean
Date   Thu, 2 Dec 2010 10:16:35 +0000

Let's spell this out. Stata is _very good_ at stuff like this. Doing it for several variables is only a little more work than doing it for one. 

foreach v of var revenue income assets { 
	egen work = mean(`v'), by(industry) 
	replace `v' = work if missing(`v')
	drop work 
} 

Notice that we -drop work- after each use. (There is no -ereplace- corresponding to -egen-.) This is a way of cutting down on the memory demand that worried Fabio. 

You could do it without -egen-. 

gen work = . 
sort industry 
foreach v of var revenue income assets { 
	by industry: replace work = sum(`v') / sum(`v' < .) 
	by industry: replace work = work[_N] 
	replace `v' = work if missing(`v') 
} 

The statistical implications of such imputation are fairly horrendous! But so far as Stata is concerned: 

For a tutorial on -by:-, see 

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step by: step
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q1/02   SJ 2(1):86--102                                  (no commands)
        explains the use of the by varlist : construct to tackle
        a variety of problems with group structure, ranging from
        simple calculations for each of several groups to more
        advanced manipulations that use the built-in _n and _N

For a tutorial on -foreach- and friends, see 

SJ-2-2  pr0005  . . . . . .  Speaking Stata:  How to face lists with fortitude
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/02   SJ 2(2):202--222                                 (no commands)
        demonstrates the usefulness of for, foreach, forvalues, and
        local macros for interactive (non programming) tasks

Note in particular that nothing obliges you to write out each variable name. You can use any congenial way of specifying varlists.

Nick 
[email protected] 

Nick Cox

Then it's a -foreach- loop. 

Fabio Zona

I apologize: I forgot to mention that I have multiple variables at the same time, that is, for example, Revenues, Income, Assets... and I have to do the same for each variable..!


*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index