Announcement

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

  • how to use "pairdata" with duplicate "ind"

    hello everyone,
    I'm trying to use "pairdata" for the first time and found a question: my individualID variable is not unique
    the example dataset looks like:

    input int (year city ind X Y)
    1 1 1 0.3 321
    1 1 2 0.7 452
    1 2 3 0.8 879
    1 2 4 0.6 897
    1 3 5 0.9 356
    1 3 6 0.5 698
    end

    I hope the dataset would look like this:

    input int (year city ind X Y) str5 pair
    1 1 1 0.3 321 "(1,2)"
    1 1 2 0.7 452 "(1,2)"
    1 2 3 0.8 879 "(1,2)"
    1 2 4 0.6 897 "(1,2)"
    1 1 1 0.3 321 "(1,3)"
    1 1 2 0.7 452 "(1,3)"
    1 3 5 0.9 356 "(1,3)"
    1 3 6 0.5 698 "(1,3)"
    1 2 3 0.8 879 "(2,3)"
    1 2 4 0.6 897 "(2,3)"
    1 3 5 0.9 356 "(2,3)"
    1 3 6 0.5 698 "(2,3)"
    end

    (Real dataset has many years, the example just listed one)

    I try to use "pairdata X Y, ind(city) fam(year)", it doesn't work because the variable "city" is duplicate.

    Any help would be immensely appreciated! Thank you!

  • #2
    Code:
    clear
    input int (year city ind X Y)
    1 1 1 0.3 321
    1 1 2 0.7 452
    1 2 3 0.8 879
    1 2 4 0.6 897
    1 3 5 0.9 356
    1 3 6 0.5 698
    2 1 1 0.3 321
    2 2 3 0.8 879
    2 2 4 0.6 897
    2 3 6 0.5 698
    end
    
    preserve
    keep city year
    contract *
    rename city city2
    tempfile 2
    save `2'
    restore
    joinby year using `2'
    drop if city==city2
    gen pairid= string(min(city, city2)) +","+ string(max(city, city2))
    drop _freq city2
    Res.:

    Code:
    
    . sort year pairid ind
    
    . l, sepby(year pairid)
    
         +--------------------------------------+
         | year   city   ind   X     Y   pairid |
         |--------------------------------------|
      1. |    1      1     1   0   321      1,2 |
      2. |    1      1     2   0   452      1,2 |
      3. |    1      2     3   0   879      1,2 |
      4. |    1      2     4   0   897      1,2 |
         |--------------------------------------|
      5. |    1      1     1   0   321      1,3 |
      6. |    1      1     2   0   452      1,3 |
      7. |    1      3     5   0   356      1,3 |
      8. |    1      3     6   0   698      1,3 |
         |--------------------------------------|
      9. |    1      2     3   0   879      2,3 |
     10. |    1      2     4   0   897      2,3 |
     11. |    1      3     5   0   356      2,3 |
     12. |    1      3     6   0   698      2,3 |
         |--------------------------------------|
     13. |    2      1     1   0   321      1,2 |
     14. |    2      2     3   0   879      1,2 |
     15. |    2      2     4   0   897      1,2 |
         |--------------------------------------|
     16. |    2      1     1   0   321      1,3 |
     17. |    2      3     6   0   698      1,3 |
         |--------------------------------------|
     18. |    2      2     3   0   879      2,3 |
     19. |    2      2     4   0   897      2,3 |
     20. |    2      3     6   0   698      2,3 |
         +--------------------------------------+

    Comment

    Working...
    X