Announcement

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

  • how to drop an observation if it does not appear in both years

    Hello!

    I have some data with firm and year and some other variables, I want to make sure each firm has data in both 2019 and 2020, if there is only 1 year of data and the other year is missing, I need to delete it. How can I do that? Thanks!

    For illustration, here are some data
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double firm float year
     1 2019
     1 2020
     2 2019
     2 2020
     4 2020
     5 2019
     5 2020
     6 2019
     6 2020
     7 2020
     8 2019
     8 2020
     9 2020
    10 2019
    11 2019
    11 2020
    12 2019
    12 2020
    14 2020
    16 2019
    17 2019
    17 2020
    19 2019
    19 2020
    20 2019
    20 2020
    format %ty year

  • #2
    Yun:
    you may want to consider:
    Code:
    . bysort id (year): drop if _N<2
    
    
    . list
    
         +-----------+
         | id   year |
         |-----------|
      1. |  1   2019 |
      2. |  1   2020 |
      3. |  2   2019 |
      4. |  2   2020 |
      5. |  5   2019 |
         |-----------|
      6. |  5   2020 |
      7. |  6   2019 |
      8. |  6   2020 |
      9. |  8   2019 |
     10. |  8   2020 |
         |-----------|
     11. | 11   2019 |
     12. | 11   2020 |
     13. | 12   2019 |
     14. | 12   2020 |
     15. | 17   2019 |
         |-----------|
     16. | 17   2020 |
     17. | 19   2019 |
     18. | 19   2020 |
     19. | 20   2019 |
     20. | 20   2020 |
         +-----------+
    
    .
    A cautionary tale is called for, though: this way you're making up your dataset, without delving into the reason(s) why some panels have one wave of data only.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment

    Working...
    X