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   Fabio Zona <>
Subject   Re: st: replacing with mean
Date   Thu, 2 Dec 2010 21:12:54 +0100 (CET)

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

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)? 


----- Messaggio originale -----
Da: "Dimitriy V. Masterov" <>
Inviato: Giovedì, 2 dicembre 2010 20:41:50 GMT +01:00 Amsterdam/Berlino/Berna/Roma/Stoccolma/Vienna
Oggetto: Re: st: replacing with mean


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:

*   For searches and help try:

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