# st: AW: How do I create variables summarizing for each individual properties of other individuals with same covariates value?

 From "Martin Weiss" To Subject st: AW: How do I create variables summarizing for each individual properties of other individuals with same covariates value? Date Thu, 5 Nov 2009 12:39:40 +0100

```<>

*************
clear*
set obs 1000
gen personid=_n

gen byte region= irecode(_n,0,1000,5000,10000)
gen income= rnormal(0,100)+1500
gen byte education=9+int(8*runiform())
gen byte activity=int(10*runiform())
gen age=20+int(10*runiform())

//generate var to be filled later on
gen meanincome=.
//also count the number of "peers"
gen count=.

qui levelsof personid

qui foreach id of numlist `r(levels)'{
tempvar touse
gen byte `touse'=region==region[`id'] & /*
*/ education==education[`id'] & /*
*/ activity==activity[`id'] & /*
*/ inrange(age, age[`id']-5, age[`id']+5)
su income if `touse', mean
replace meanincome=r(mean) if `touse'
replace count=r(N) if `touse'
}
*************

HTH
Martin

-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Vincenzo
Carrieri
Gesendet: Donnerstag, 5. November 2009 12:24
An: statalist@hsphsun2.harvard.edu
Betreff: st: How do I create variables summarizing for each individual
properties of other individuals with same covariates value?

Dear Statalisters,
I have this kind of problem.

given this 5 variables:

education, age, region, activity, income
I would like to generate a variable that is the mean of income across
people with:
1) same education
2) the same age group, within 5 years younger or older than the
individual concerned
3) same region
4) same activity

Then I would like to assign the mean calculated to the person that has
the same covariates value. I think that the problems could be solved
with some stata commands as foreach, but I am not able to set the
problem in stata.

How to do this with stata?

Vincenzo
*
*   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/

*
*   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/
```