Announcement

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

  • Dropping a duplicate in variable 1 if it was also different in variable 2

    Hello,
    I am new to stata and need help in the following :
    Variable 1 ( ID) Variable 2 (color)
    1 red
    1 red
    2 blue
    2 green
    3 purple
    4 red
    Example :
    Within duplicates of ID , I want to drop the IDs where the duplicates are different in color but keep IDs that have duplicates with the same color .
    i.e.
    I want to drop both duplicates with ID 2 because one is blue and the other is green
    I want to keep duplicates for ID 1 because both duplicates have red color
    Thank you !

  • #2
    Heba:
    welcome to this forum.
    As a bit of surgery at this end since you forgot to use -dataex- to share your data excerpt/example, you may want to try:
    Code:
    . egen wanted=group( Var_A Var_B)
    
    . list
    
         +-------------------------+
         | Var_A    Var_B   wanted |
         |-------------------------|
      1. |     1      red        1 |
      2. |     1      red        1 |
      3. |     2     blue        2 |
      4. |     2    green        3 |
      5. |     3   purple        4 |
         |-------------------------|
      6. |     4      red        5 |
         +-------------------------+
    
    . bysort Var_A: drop if wanted[_n]!=wanted[_N]
    (1 observation deleted)
    
    . list
    
         +-------------------------+
         | Var_A    Var_B   wanted |
         |-------------------------|
      1. |     1      red        1 |
      2. |     1      red        1 |
      3. |     2    green        3 |
      4. |     3   purple        4 |
      5. |     4      red        5 |
         +-------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Heba:
      if you want to -drop- the whole panel for which the condition is satisfied:
      Code:
      bysort Var_A: drop if wanted[_N]!=wanted[_N-1] &_N==2
      (2 observations deleted)
      
      . list
      
           +-------------------------+
           | Var_A    Var_B   wanted |
           |-------------------------|
        1. |     1      red        1 |
        2. |     1      red        1 |
        3. |     3   purple        4 |
        4. |     4      red        5 |
           +-------------------------+
      
      .
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Thank you very much Carlo !
        This was very helpful.

        Comment

        Working...
        X