Announcement

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

  • How to keep id that have two observations

    Hello, I want to keep HHPBASE that has both dup 1 and 2. How do I delete observations that just have one copy in order to create a panel

    bys HHPBASE : gen dup=_n

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double HHPBASE float dup
    101020102009 1
    101020102009 2
    101020103002 2
    101020105003 1
    101020116004 1
    101020116004 2
    101020117001 1
    101020118005 1
    101020120004 1
    101020120004 2
    101020204002 1
    101020206004 1
    101020206004 2
    101020207004 1
    101020207004 2
    101020214002 1
    101020215002 1
    101020215002 2
    101020216002 2
    101020217002 1
    101020217002 2
    101020220002 1
    101020305002 1
    101020306009 1
    101020306009 2
    101020307002 1
    101020310004 1
    101020311002 1
    101020314004 1
    101020314004 2
    101020316002 1
    101020319004 1
    101020319004 2
    101020401002 1
    101020401002 2
    101020404002 1
    101020408004 1
    101020409002 1
    101020413017 2
    101020415002 1
    101020417011 1
    101020417011 2
    101020501002 1
    101020504004 1
    101020507004 1
    101020507004 2
    101020512003 1
    101020515002 1
    101020517002 2
    101020518002 2
    101020520004 1
    101020520004 2
    101020601002 1
    101020603002 1
    101020603002 2
    101020604002 1
    101020606002 1
    101020611002 1
    101020612004 1
    101020612004 2
    101020615004 1
    101020615004 2
    101020701002 1
    101020701002 2
    101020702002 1
    101020705002 2
    101020706004 1
    101020706004 2
    101020707002 1
    101020710002 1
    101020711002 1
    101020712002 1
    101020803003 1
    101020806002 1
    101020807002 2
    101020809002 2
    101020812002 1
    101020812002 2
    101020813002 2
    101020814002 2
    101030101004 1
    101030102002 1
    101030104002 2
    101030105003 1
    101030109004 1
    101030202002 2
    101030205004 1
    101030207002 2
    101030213002 2
    101030214002 1
    101030214002 2
    101030311004 1
    101030311004 2
    101030315004 1
    101030315004 2
    101030408002 2
    101030411002 2
    101030412002 2
    101030414002 1
    101030414002 2
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 18233 observations
    Use the count() option to list more


  • #2
    Raphael:
    I'd try (not tested, though):
    Code:
    bysort HHPBASE (dup): keep if _N==2 & dup[1]!=dup[2]
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Carlo Lazzaro 's code should work fine if, as stated, the values can only be 1 and 2. If other values are possible, you may need to be more explicit:

      Code:
      bysort HHPBASE (dup): keep if _N==2 & dup[1] == 1 & dup[2] == 2
      or
      Code:
        
      bysort HHPBASE (dup): keep if _N==2 & dup == _n 

      Comment


      • #4
        Thank you for the help

        Comment

        Working...
        X