Announcement

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

  • Combinations without replacement

    Hello statalist community, I need your kind help in some code.
    Unique ID Person Person's toffees Unique ID Friend Friend's toffees
    A 2 B 5
    B 5 A 2
    C 9 . .
    D 5 . .
    E 7 F 17
    F 17 E 7
    G 11 . .
    H 13 I 1
    I 1 H 13
    J 25 . .
    I want to subset this data set, so that I am left with the combinations of ("Unique ID Person", Unique ID Friend") without replacement. Which implies red rows are turned into missing values.


    Or, so that I am left with the combinations ("Unique ID Person", Unique ID Friend")---> ( "A", "B") ( "C", ".") ( "D", ".") ( "E", "F") ( "G", ".") ( "H", "I") ( "J", ".")
    Combinations of ("Unique ID Person", Unique ID Friend") keep/don't keep
    ( "A", "B") keep
    ("B", "A") turn it in missing value
    ( "C", ".") keep
    ( "D", ".") keep
    ( "E", "F") keep
    ("F", "E") turn it in missing value
    ( "G", ".") keep
    ( "H", "I") keep
    ("I", "H") turn it in missing value
    ( "J", ".") keep
    Last edited by ajay pasi; 01 Nov 2022, 03:29.

  • #2
    Here is some technique. More at https://www.stata-journal.com/articl...article=dm0043

    Run the code to see what it does. By the way, your data example was helpful but still needed rocket surgery to make it a good example for Stata. Please use dataex !


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str1 PersonId byte PersonToffees str1 FriendId byte FriendToffees
    "A"  2 "B"  5
    "B"  5 "A"  2
    "C"  9 "."  .
    "D"  5 "."  .
    "E"  7 "F" 17
    "F" 17 "E"  7
    "G" 11 "."  .
    "H" 13 "I"  1
    "I"  1 "H" 13
    "J" 25 "."  .
    end
    
    gen Id1 = cond(PersonId < FriendId, PersonId, FriendId)
    gen Id2 = cond(FriendId < PersonId, PersonId, FriendId)
    
    duplicates report Id? *Toffees
    
    sort Id?
    order Id?
    list, sepby(Id?)
    
    duplicates drop Id?, force
    
    list
    Last edited by Nick Cox; 01 Nov 2022, 03:36.

    Comment


    • #3
      Sorry for the inconvenience Nick. Thanks for your help, appreciate that.

      Comment


      • #4
        Note that #1 ignores advice already given in https://www.statalist.org/forums/for...matching-cases

        Comment


        • #5
          Noted. Thanks Nick.

          Comment

          Working...
          X