Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Drop (Keep) observations

    Dear experts,

    I'm wondering how to drop (or keep) my observations. Below is my dataset. What I wanted to do is this:

    If in a particular year there are two observations with adjustment shows "0" and "1", keep the observation with adjustment "1" while dropping the observation with adjustment "0". For example, in Year 2008, drop the first observation but keep the second.

    If in a particular year, there is only one observation with adjustment shows "0", I'll just keep this observation. For example, there is only one observation with adjustment "0" in the year 2014, so just keep it.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    firm        year  adjustment  revenue
    300269 "2008-12-31" 0  184107248
    300269 "2008-12-31" 1  184107248
    300269 "2009-12-31" 0  254609552
    300269 "2009-12-31" 1  254609552
    300269 "2010-12-31" 0  349930400
    300269 "2010-12-31" 1  349930400
    300269 "2011-12-31" 0  512150592
    300269 "2011-12-31" 1  512150592
    300269 "2012-12-31" 0  534164544
    300269 "2012-12-31" 1  534164544
    300269 "2013-12-31" 0  585608128
    300269 "2013-12-31" 1  585608128
    300269 "2014-12-31" 0  970097984
    300269 "2015-12-31" 0 1522596480
    300269 "2016-12-31" 0 2803457792
    300269 "2017-12-31" 1 3952366336
    300269 "2018-12-31" 0 4053373440
    300269 "2018-12-31" 1 4053373440
    end
    Thank you very much for helping!
    Last edited by Ke Gong; 01 Nov 2019, 20:33.

  • #2
    Try this:
    Code:
    assert inlist(adjustment, 0, 1)                    // verify that adjustment is always 0 or 1
    bysort firm year (adjustment):  assert  _N <= 2    // verify that there are at most 2 observations per firm year
    by     firm year (adjustment):  keep if _n == _N

    Comment


    • #3
      West Addison Your code works pretty well. Thank you very much!!

      Comment

      Working...
      X