1
votes

I have the following dataset that tells me if there is a connection between two people:

pos_1    pos_2
  2        4
  2        5
  1        2
  3        9
  4        2
  9        3

The above is read as person_2 is connected to person_4,...,person_4 is connected to person_2, and person_9 is connected to person_3

And I want to create a third dichotomous variable recip that lets me know if the connection is reciprocated, in other words if person_X is connected to person_Y is person_Y connected to person_X leaving me with:

pos_1    pos_2    recip
  2        4        1
  2        5        0
  1        2        0
  3        9        1
  4        2        1
  9        3        1
1

1 Answers

3
votes

2,4 and 4,2 can be seen to be reciprocal as both sort to 2,4. min() and max() together are sufficient to do that. Use a space or other punctuation to disambiguate e.g. 1,23 and 12,3.

gen pair = string(min(pos_1, pos_2)) + " " + string(max(pos_1, pos_2)) 
bysort pair : gen recip = _N == 2 

See Cox, N.J. 2008. The problem of split identity, or how to group dyads. Stata Journal 8: 588-591, which is accessible at http://www.stata-journal.com/sjpdf.html?articlenum=dm0043

This also offers a data check, as each pair should appear once or twice, but no more:

by pair: assert _N == 1 | _N == 2 

or

duplicates report pair