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 15:25:51 -0500
Fabio,
Since you have your own data, you should use:
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
}
}
This code assumes that you have data that looks like this:
industry listA Var1 Var2 Var3
A 1 0 1 1
A 2 0 1 1
A 3 1 1 1
A 4 1 0 0
A 5 1 0 0
A 6 1 0 2
A 7 1 1 2
A 8 0 0 2
A 9 1 1 1
I was trying to use the variable names from your original e-mail as
much as I could. If your variables have different names (including
capitalization: Var1 is not the same as var1), you many need to modify
my code to reflect that.
DVM
On Thu, Dec 2, 2010 at 3:12 PM, Fabio Zona <[email protected]> wrote:
> Hi Dimitry, that's great! Thank you very much!
>
> I tried to implement your command; however, I get an error as it says:
>
>
> variable Var* not found
> r(111);
>
>
> Might it depend on the fact that I start your procedure from here:
>
> levelsof listA, local(listA)
>
> as I already have my dataset ready for calculation)?
>
> F
>
>
>
>
> ----- Messaggio originale -----
> Da: "Dimitriy V. Masterov" <[email protected]>
> A: [email protected]
> Inviato: Giovedì, 2 dicembre 2010 20:41:50 GMT +01:00 Amsterdam/Berlino/Berna/Roma/Stoccolma/Vienna
> Oggetto: Re: st: replacing with mean
>
> 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/
>
> *
> * 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/