palpen
10/1/2016 - 7:01 AM

Drops observations in a panel data that exceed a given percentile of a variable in a given year

Drops observations in a panel data that exceed a given percentile of a variable in a given year

capture program drop panel_trim

program define panel_trim

    args id yrvar v yr pct

    display "`id' `yrvar' `v' `yr' `pct'"

    gen `v'_`yr' = `v' if `yrvar' == `yr'

    sort `id' `yrvar'
    bysort `id': replace `v'_`yr' = `v'_`yr'[_n + 1] if missing(`v'_`yr'[_n - 1])

    qui sum `v' if `yrvar' == `yr', d
    gen `v'_`pct' = r(`pct')
    drop if `v'_`yr' > `v'_`pct'

end

// usage (drops firms with panel id firm_id whose total assets in 2010 exceeds the 99th percentile)
panel_trim firm_id year total_asset 2010 p99