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
[email protected]
*
* 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/