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

st: RE: calculating means over time


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: calculating means over time
Date   Mon, 21 Oct 2002 17:39:15 +0100

Maureen Paul

> I am trying to efficiently generate the mean of a few variables for
> each individual over time. I have done the following but it does not
> seem to do what I intend. Can anyone help me out here?
> Thanks. So just
> to make the problem clearer, I have a panel of individuals and would
> like to calculate for each individual over time, the mean
> of, say, age
> wage and number of children.
>
> mac def y "age wage children"
> for var $y: by(pid): egen sumX=sum(X)
> for var $y: egen zX=mean(sumX)


-mac def- is an old, but still viable, way of
defining macros.

The main problem is likely to be in your use of -by-.
You need to -sort- first, or use -bysort-.

My guess is that this should work in Stata 7:

mac def y "age wage children"
for var $y: bysort pid: egen sumX=sum(X)
for var $y: egen zX=mean(sumX)

However, you are putting a single value
in each of the zX. From what you say,
you want something more like this:

mac def y "age wage children"
for var $y: bysort pid: egen meanX=mean(X)

A modern way of doing this might be

foreach v of var age wage children {
	bysort pid : egen sum`v' = sum(`v')
}

which Stata will execute much more
efficiently. What's more, it extends
much more easily to more complicated problems.

Nick
n.j.cox@durham.ac.uk

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index