Announcement

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

  • Dropping duplicates but keeping recent observations

    I have a problem where I have duplicate observations that have different hire dates and each hire date has a different salary listed. For example:

    Name Hire date Salary
    Maria Johnson, 04/21/2016, $21,450
    Maria Johnson, 09/15/2016, $21,450
    maria Johnson, 03/12/2017, $23,640

    How do I drop the duplicates by name but only keep the most recent hire date and current salary?

  • #2
    I have tried this code:

    sort name
    duplicates drop name, force

    But it only keeps the first observation which isn't the recent one all the time.

    Comment


    • #3
      Code:
      by name (hire_date), sort: keep if _n == _N
      In the future, please use the -dataex- command to show example data. Indeed, what you show does not even come from a Stata data set--it can't because variable names cannot contain blanks. If you haven't even imported your data to Stata yet, it is premature to ask for help with coding an analysis. Layouts of the type you show can be difficult to bring in to Stata to test out a solution , and even when they import easily, they don't provide information about data storage types, labeling, formats, etc., that are sometimes crucial to getting the solution correct. When you use -dataex- to show a Stata example, you enable those who want to help you to create a complete and faithful replica of your Stata data example, leaving nothing to the imagination, with a simple copy/paste operation. That makes it possible to test out solutions and verify that they actually work in your situation. If you are running Stata version 15.1 -dataex- is part of your setup. If you are running an earlier Stata, you can get -dataex- by running -ssc intall dataex-.

      Comment


      • #4
        On #3 there should not be any surprise. You're dropping duplicates on name and telling Stata that it should force that regardless of other variables.

        Comment

        Working...
        X