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]
Re: st: replacing with mean
From
"Dimitriy V. Masterov" <[email protected]>
To
[email protected]
Subject
Re: st: replacing with mean
Date
Thu, 2 Dec 2010 14:41:50 -0500
Fabio,
Nick's suggestion can be implemented in the 2nd loop below:
************************************************************
clear all
set more off
/* Fake Data */
set obs 26
gen str1 industry=""
local c=1
foreach l in `c(ALPHA)' {
replace industry = "`l'" in `c'
local ++c
}
expand 27
sort industry
forvalues v=1/20 {
gen Var`v'=round(uniform()*`v',1)
}
gen listA=_n
/* Actual Loop */
levelsof listA, local(listA)
foreach var of varlist Var* {
qui gen `var'_med=.
foreach firm in `listA' {
qui gen inc=cond(listA != `firm',1,.)
qui egen med=median(`var'*inc), by(industry)
qui replace `var'_med=med if listA==`firm'
qui drop med inc
}
}
**************************************************************
*
* 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/