Announcement

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

  • How to create unique identifiers (IDs) for each observation

    Hi there, I am analyzing some data at the dyadic level, and would like to create some identifiers columns to represent the actors, the partners, and their dyads.

    The raw data table goes like this:
    teamID actorName partnerName
    1 Adam Raddi
    1 Adam Samatha
    1 Adam JoJo
    1 Raddi Adam
    1 Raddi Samatha
    1 Raddi JoJo
    1 Samatha Adam
    1 Samatha Raddi
    1 Samatha JoJo
    1 JoJo Adam
    1 JoJo Raddi
    1 JoJo Samatha
    2 Nix Kim
    2 Nix Susan
    2 Kim Nix
    2 Kim Susan
    2 Susan Nix
    2 Susan Kim
    I would like to have a table with the ID columns as the one below. there are some requirements for the ID columns.

    1. Each actor or partner should have a unique ID.

    2. The actor_ID should correspond with the partner_ID. For example, when Adam is the "actor", his actor_ID is "1", thus, when Adam becomes the "partner", his partner_ID should still be "1". This example is highlighted in red below. The example in red.

    3. Each dyad should have a unique ID regardless of the order of actor and partner. That is, for the dyad of Adam and Raddi, the dyad_ID is "1" no matter (Adam is the actor and Raddi is the partner) or (Raddi is the actor and Adam is the partner). This example is highlighted in green.

    Does anyone know how to generate these ID columns?
    teamID actorName partnerName actor_ID patner_ID dyad_ID
    1 Adam Raddi 1 2 1
    1 Adam Samatha 1 3 2
    1 Adam JoJo 1 4 3
    1 Raddi Adam 2 1 1
    1 Raddi Samatha 2 3 4
    1 Raddi JoJo 2 4 5
    1 Samatha Adam 3 1 2
    1 Samatha Raddi 3 2 4
    1 Samatha JoJo 3 4 6
    1 JoJo Adam 4 1 3
    1 JoJo Raddi 4 2 5
    1 JoJo Samatha 4 3 6
    2 Nix Kim 5 6 7
    2 Nix Susan 5 7 8
    2 Kim Nix 6 5 7
    2 Kim Susan 6 7 9
    2 Susan Nix 7 5 8
    2 Susan Kim 7 6 9
    Last edited by Victor Smith; 22 Mar 2018, 01:35.

  • #2
    Good news: all previously discussed here.

    To get consistent numeric identifiers for your name variables, use multencode from SSC, as first announced here

    https://www.stata.com/statalist/arch.../msg00729.html

    and discussed several times since the forum started, most recently in

    https://www.statalist.org/forums/for...es-with-encode

    The general territory of identifiers for dyads was discussed in http://www.stata-journal.com/sjpdf.h...iclenum=dm0043

    that is

    SJ-8-4 dm0043 . Tip 71: The problem of split identity, or how to group dyads
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q4/08 SJ 8(4):588--591 (no commands)
    tip on how to handle dyadic identifiers


    and dm0043 is thus revealed as an otherwise unpredictable search term to find related discussions here.

    That said, how does this work on your example?

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte teamid str7(actorname partnername)
    1 "Adam"    "Raddi"  
    1 "Adam"    "Samatha"
    1 "Adam"    "JoJo"  
    1 "Raddi"   "Adam"  
    1 "Raddi"   "Samatha"
    1 "Raddi"   "JoJo"  
    1 "Samatha" "Adam"  
    1 "Samatha" "Raddi"  
    1 "Samatha" "JoJo"  
    1 "JoJo"    "Adam"  
    1 "JoJo"    "Raddi"  
    1 "JoJo"    "Samatha"
    2 "Nix"     "Kim"    
    2 "Nix"     "Susan"  
    2 "Kim"     "Nix"    
    2 "Kim"     "Susan"  
    2 "Susan"   "Nix"    
    2 "Susan"   "Kim"    
    end
    
    * type "ssc install multencode" to install
    multencode actorname partnername, gen(actor_id partner_id)
    
    gen dyad_id = cond(actor_id < partner_id, string(actor_id) + " " + string(partner_id), string(partner_id) + " " + string(actor_id))
    egen dyad = group(dyad_id), label
    
    list, sepby(teamid)
    
         +---------------------------------------------------------------------+
         | teamid   actorn~e   partne~e   actor_id   partne~d   dyad_id   dyad |
         |---------------------------------------------------------------------|
      1. |      1       Adam      Raddi       Adam      Raddi       1 5    1 5 |
      2. |      1       Adam    Samatha       Adam    Samatha       1 6    1 6 |
      3. |      1       Adam       JoJo       Adam       JoJo       1 2    1 2 |
      4. |      1      Raddi       Adam      Raddi       Adam       1 5    1 5 |
      5. |      1      Raddi    Samatha      Raddi    Samatha       5 6    5 6 |
      6. |      1      Raddi       JoJo      Raddi       JoJo       2 5    2 5 |
      7. |      1    Samatha       Adam    Samatha       Adam       1 6    1 6 |
      8. |      1    Samatha      Raddi    Samatha      Raddi       5 6    5 6 |
      9. |      1    Samatha       JoJo    Samatha       JoJo       2 6    2 6 |
     10. |      1       JoJo       Adam       JoJo       Adam       1 2    1 2 |
     11. |      1       JoJo      Raddi       JoJo      Raddi       2 5    2 5 |
     12. |      1       JoJo    Samatha       JoJo    Samatha       2 6    2 6 |
         |---------------------------------------------------------------------|
     13. |      2        Nix        Kim        Nix        Kim       3 4    3 4 |
     14. |      2        Nix      Susan        Nix      Susan       4 7    4 7 |
     15. |      2        Kim        Nix        Kim        Nix       3 4    3 4 |
     16. |      2        Kim      Susan        Kim      Susan       3 7    3 7 |
     17. |      2      Susan        Nix      Susan        Nix       4 7    4 7 |
     18. |      2      Susan        Kim      Susan        Kim       3 7    3 7 |
         +---------------------------------------------------------------------+
    
     list, sepby(teamid) nolabel
    
         +---------------------------------------------------------------------+
         | teamid   actorn~e   partne~e   actor_id   partne~d   dyad_id   dyad |
         |---------------------------------------------------------------------|
      1. |      1       Adam      Raddi          1          5       1 5      2 |
      2. |      1       Adam    Samatha          1          6       1 6      3 |
      3. |      1       Adam       JoJo          1          2       1 2      1 |
      4. |      1      Raddi       Adam          5          1       1 5      2 |
      5. |      1      Raddi    Samatha          5          6       5 6      9 |
      6. |      1      Raddi       JoJo          5          2       2 5      4 |
      7. |      1    Samatha       Adam          6          1       1 6      3 |
      8. |      1    Samatha      Raddi          6          5       5 6      9 |
      9. |      1    Samatha       JoJo          6          2       2 6      5 |
     10. |      1       JoJo       Adam          2          1       1 2      1 |
     11. |      1       JoJo      Raddi          2          5       2 5      4 |
     12. |      1       JoJo    Samatha          2          6       2 6      5 |
         |---------------------------------------------------------------------|
     13. |      2        Nix        Kim          4          3       3 4      6 |
     14. |      2        Nix      Susan          4          7       4 7      8 |
     15. |      2        Kim        Nix          3          4       3 4      6 |
     16. |      2        Kim      Susan          3          7       3 7      7 |
     17. |      2      Susan        Nix          7          4       4 7      8 |
     18. |      2      Susan        Kim          7          3       3 7      7 |
         +---------------------------------------------------------------------+
    The numbers are not all the same, but the spirit of identifiers is what I think you're asking for.

    Comment


    • #3
      Thanks Nick!

      Comment

      Working...
      X