Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: replacing with mean

From   "Dimitriy V. Masterov" <>
Subject   Re: st: replacing with mean
Date   Thu, 2 Dec 2010 12:05:16 -0500


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

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


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


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:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index