Announcement

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

  • Generating IDs

    Dear all,

    Suppose I have the following dataset

    Code:
    clear
    input var1 var2
    1 1
    1 2
    1 3
    1 4
    2 1
    2 2
    2 3
    2 4
    3 1
    3 2
    3 3
    3 4
    4 1
    4 2
    4 3
    4 4
    end
    I'd like to create a list of IDs based on var1 and var2. In particular, I'd like to get the same ID when the observations in var1 and var2 are swapped. For example, var1=3 and var2=4 should have the same ID as where var1=4 and var2=3.

    I have tried
    Code:
     egen id = group(var1 var2)
    but it doesn't not give me exactly what I'm looking for.

    Could anyone please help me with this?

    Thank you very much for your help,

    Vinh
    Last edited by Vinh Ng; 26 Mar 2023, 19:37.

  • #2
    Code:
    gen sorted_pair = string(min(var1, var2)) + ";" + string(max(var1,var2))
    egen wanted = group(sorted_pair), label
    drop sorted_pair

    Comment


    • #3
      Many thanks for your help, Clyde. Your code works perfectly.

      Cheers,

      Vinh

      Comment


      • #4
        See also https://www.stata-journal.com/articl...article=dm0043 for a broader discussion.

        Comment


        • #5
          Thank you for the link, Nick.

          Cheers,

          Vinh

          Comment

          Working...
          X