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

# Re: st: Creating a Variable for the average by subgroup for a panel dataset

 From Jeph Herrin To statalist@hsphsun2.harvard.edu Subject Re: st: Creating a Variable for the average by subgroup for a panel dataset Date Fri, 28 Dec 2012 09:54:34 -0500

Making some assumptions about your variable names, this should work:

bysort sex agecat state : egen avgincome=mean(income)

Jeph

On 12/28/2012 8:43 AM, gianluca manzi wrote:
Dear Statalist,

How can I calculate the average of a variable by the subgroup of
several other variables?

I have a panel dataset of individuals incomes, the state they live in,
their ages and genders.  I want to calculate the average income for
individual i at time period t based on these characteristics and
create a variable for this (to use as a "reference income" individual
i in time t will compare his income to).

So (say) I want to create a variable that will calculate the average
income for all women aged 20-30 who live in California;  the average
income for all men aged 20-30 who live in California;  the average
income for all women aged 30-40 who live in California;  the average
income for all men aged 30-40 who live in California;  the average
income for all women aged 20-30 who live in Texas etc etc.

The purpose of this is to create a "reference income" which individual
i in period t will compare his/her income to. So if the individual is
woman, aged 22, who lives in California, she'll compare her income to
the average income of 20-30 year old women who live in California. How
do I create this variable?

Thank you!
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/