Bookmark and Share

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 12:05:16 -0500

Fabio,

I got this to work with a small fake dataset and 2 variables. The
solution is not very elegant in that it involves looping over firms
and creating many variables for each firm, so it may not work for some
large number of firms that depends on your computer's memory and Stata
flavor.

I hope you will find the logic of this approach useful.

DVM

************************************************************
clear all

set obs 8

input str1 industry listA
A   1		
A	2		
A	3
B	4
B	5
B	6
C	7
C	8

gen x1=round(uniform()*5,1)
gen x2=round(uniform()*10,1)

replace x1=. in 8
replace x2=. in 7

list

tempfile data
save `data'



levelsof listA, local(listA)

foreach firm in `listA' {
	foreach var of varlist x1 x2 {
		gen `var'_mod`firm'=cond(listA!=`firm',1,.) * `var'
	}
}

collapse (p50) *_mod*, by(industry)

reshape long x1_mod x2_mod, i(industry) j(listA)

merge 1:1 industry listA using `data', keep(match) nogen

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index