Announcement

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

  • Delete duplicates including the original one

    Hi,
    I have been trying to delete observations, including the original one, in stata. I could not find any code. The 'drop duplicates var' deletes the duplicate observations if any, but keeps the first observation. BUT, I want to remove all observations that have duplicates, including the first one. Please help.

  • #2
    without more, it is hard to be concrete but here is a strategy - use -duplicates tag- to tag one of the duplicates and then use -egen- with the max option to spread that tag to all relevant observations and then drop those so tagged (you can then drop the tag variable and the variable created by -egen-)

    Comment


    • #3
      In #1 drop duplicates should be duplicates drop

      duplicates
      isn't designed for what you want, even as a special or limiting case: but what you want is easy. For the variables concerned you write

      Code:
      bysort varlist : drop if _N > 1
      Here's a prototype, but watch out. This can be terribly destructive.


      Code:
      . clear
      
      . set obs 10
      number of observations (_N) was 0, now 10
      
      . gen foo = word("a b c d e", _n) in 1/5
      (5 missing values generated)
      
      . replace foo = "f" in 6/L
      (5 real changes made)
      
      .
      . list
      
           +-----+
           | foo |
           |-----|
        1. |   a |
        2. |   b |
        3. |   c |
        4. |   d |
        5. |   e |
           |-----|
        6. |   f |
        7. |   f |
        8. |   f |
        9. |   f |
       10. |   f |
           +-----+
      
      .
      . bysort foo : drop if _N > 1
      (5 observations deleted)
      
      .
      . list
      
           +-----+
           | foo |
           |-----|
        1. |   a |
        2. |   b |
        3. |   c |
        4. |   d |
        5. |   e |
           +-----+

      Comment

      Working...
      X