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]

From |
Nick Cox <n.j.cox@durham.ac.uk> |

To |
"'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |

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 n.j.cox@durham.ac.uk 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/

**References**:**Re: st: replacing with mean***From:*"Michael N. Mitchell" <Michael.Norman.Mitchell@gmail.com>

**Re: st: replacing with mean***From:*Fabio Zona <fabio.zona@unibocconi.it>

**RE: st: replacing with mean***From:*Nick Cox <n.j.cox@durham.ac.uk>

- Prev by Date:
**st: Predicting number of cases** - Next by Date:
**st: Easy estimation methods for discrete-time duration models** - Previous by thread:
**RE: st: replacing with mean** - Next by thread:
**Re: st: replacing with mean** - Index(es):