Announcement

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

  • how to delete duplicates by unit across group

    Hello,

    I have the following dataset:

    individual group
    1111 0
    1111 0
    1111 1
    1111 1
    2222 1
    2222 1
    3213 0
    3213 0
    3213 0
    3213 1
    3213 1
    3213 1
    3213 1
    3213 1
    3444 0
    3444 0
    3444 0

    In case the individual is both in group 0 and 1 (e.g. 1111, 3213) I would like to delete all observations related to that individual from group 0. For example, I would like to end up with:

    individual group
    1111 1
    1111 1
    2222 1
    2222 1
    3213 1
    3213 1
    3213 1
    3213 1
    3213 1
    3444 0
    3444 0
    3444 0

    Does anyone know a quick way to do this? Thank you.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int individual byte group
    1111 0
    1111 0
    1111 1
    1111 1
    2222 1
    2222 1
    3213 0
    3213 0
    3213 0
    3213 1
    3213 1
    3213 1
    3213 1
    3213 1
    3444 0
    3444 0
    3444 0
    end
    bysort individual (group): drop if group[_N]==1 & group==0
    list, sepby(individual) abbreviate(12)
    Code:
    . list, sepby(individual) abbreviate(12)
    
         +--------------------+
         | individual   group |
         |--------------------|
      1. |       1111       1 |
      2. |       1111       1 |
         |--------------------|
      3. |       2222       1 |
      4. |       2222       1 |
         |--------------------|
      5. |       3213       1 |
      6. |       3213       1 |
      7. |       3213       1 |
      8. |       3213       1 |
      9. |       3213       1 |
         |--------------------|
     10. |       3444       0 |
     11. |       3444       0 |
     12. |       3444       0 |
         +--------------------+

    Comment


    • #3
      Thanks very much William Lisowski - it works perfectly.

      Comment

      Working...
      X