Announcement

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

  • How to drop observations?

    Hello Statalist members,

    I have a question about how to drop multiple observations from the same ID (or only keep one observation per ID). For example, my current data looks like this:

    ID. Year
    1 2010
    1 2012
    1 2014
    2. 2008
    3. 2011
    3. 2014
    4. 2007
    4. 2009
    4. 2012
    5. 2007

    I only want to keep the observation for the earliest Year (or drop the observations for the later Years) and I hope my new data would look like this:
    ID. Year
    1. 2010
    2 2008
    3 2011
    4 2007
    5 2007

    Could you please let me know what code should I use?

    Many thanks
    Yuchen

  • #2
    Code:
    by ID (Year), sort: keep if _n == 1
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Code:
      by ID (Year), sort: keep if _n == 1
      In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
      Will do. Thank you so much for your help Clyde!

      Comment

      Working...
      X