Coming late to this party....
An early reaction was to use tag() but that won't work because you are counting distinct values in the union of two variables.
Here is a first principles approach. without using reshape
An early reaction was to use tag() but that won't work because you are counting distinct values in the union of two variables.
Here is a first principles approach. without using reshape
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long gid int(year ccode_GID) long(actor_id actor_id2)
62356 1997 216 . .
62357 1997 216 . .
79599 1997 216 . .
79600 1997 216 . .
79601 1997 216 . .
80317 2012 216 2082 393
80317 2014 216 2082 393
80317 2015 216 2515 393
80317 2013 216 1795 393
80317 2012 216 2082 393
80317 2017 216 2515 393
80317 2016 216 2515 393
80317 2012 216 2082 393
80317 2016 216 2515 393
80317 2012 216 2082 393
80317 2012 216 2082 1911
80318 2012 216 2082 1911
80318 2009 216 2082 393
80318 2015 216 2515 393
80318 2012 216 2082 393
80318 2017 216 2515 393
80318 2012 216 2082 1911
80318 2002 216 2515 393
80318 2015 216 1258 393
80318 2012 216 2082 1911
80318 2014 216 2515 393
80318 1997 216 2515 393
80318 2012 216 2082 393
80318 2003 216 2515 393
80318 1998 216 2515 393
80318 2004 216 2515 392
80318 2015 216 2082 1911
80318 2012 216 32 21
80318 2013 216 2622 335
80318 2012 216 2082 1911
80318 2012 216 2082 1911
80318 2007 216 2082 1822
80318 2012 216 2082 1911
80318 2012 216 2082 1911
80318 1998 216 2515 393
80318 2014 216 2515 393
80318 2012 216 2082 1911
80318 2014 216 2515 393
80318 2017 216 2515 393
80318 2012 216 2082 1911
80318 2013 216 2515 393
80318 1997 216 2515 393
80318 2003 216 2515 393
80318 2013 216 1795 393
80318 2015 216 2082 1822
80318 2012 216 2515 393
80318 2013 216 2515 393
80319 2012 216 2082 1911
80319 2012 216 2082 1911
80320 1997 216 . .
80321 1997 216 . .
80322 1997 216 . .
80323 1997 216 . .
80324 1997 216 . .
80325 1997 216 . .
80326 1997 216 . .
80327 2012 216 2082 1911
80327 2012 216 2082 1911
80327 2012 216 2082 1911
80328 1997 216 . .
80329 1997 216 . .
80330 2004 216 2515 393
80331 1997 216 . .
80332 1997 216 . .
81037 2012 216 2082 408
81037 2003 216 2515 1727
81037 2000 216 2515 393
81037 2014 216 2515 393
81037 1998 216 1909 393
81037 2004 216 2515 1727
81037 2002 216 2515 393
81037 2012 216 2082 1911
81037 1999 216 2515 393
81037 2015 216 2082 1822
81037 1999 216 33 2095
81037 2002 216 2082 393
81037 2013 216 2082 361
81037 2015 216 2082 1911
81037 2014 216 2515 393
81037 1998 216 1909 393
81037 2000 216 2515 393
81037 2016 216 33 393
81037 2000 216 2515 393
81037 1999 216 2515 361
81037 2016 216 2082 381
81037 2000 216 2515 393
81037 2014 216 2515 393
81037 1998 216 1909 393
81037 2017 216 2548 392
81037 2000 216 2515 393
81037 2001 216 2515 393
81037 2014 216 2515 393
81037 1998 216 2515 393
81037 2012 216 2082 1822
81037 2014 216 2515 393
end
gen wanted = 0
gen this = 0
egen group = group(gid year) , label
su group, meanonly
forval g = 1/`r(max)' {
quietly replace this = group == `g'
mata : data = st_data(., ("actor_id actor_id2"), "this")
mata : obs = rows(data)
mata : data = data[,1] \ data[,2]
mata : data = select(data, (data :< .))
mata : count = J(obs, 1, rows(uniqrows(data)))
mata : st_store(., "wanted", "this", count)
}
tabdisp group, c(wanted)
-----------------------
group(gid |
year) | wanted
-----------+-----------
62356 1997 | 0
62357 1997 | 0
79599 1997 | 0
79600 1997 | 0
79601 1997 | 0
80317 2012 | 3
80317 2013 | 2
80317 2014 | 2
80317 2015 | 2
80317 2016 | 2
80317 2017 | 2
80318 1997 | 2
80318 1998 | 2
80318 2002 | 2
80318 2003 | 2
80318 2004 | 2
80318 2007 | 2
80318 2009 | 2
80318 2012 | 6
80318 2013 | 5
80318 2014 | 2
80318 2015 | 6
80318 2017 | 2
80319 2012 | 2
80320 1997 | 0
80321 1997 | 0
80322 1997 | 0
80323 1997 | 0
80324 1997 | 0
80325 1997 | 0
80326 1997 | 0
80327 2012 | 2
80328 1997 | 0
80329 1997 | 0
80330 2004 | 2
80331 1997 | 0
80332 1997 | 0
81037 1998 | 3
81037 1999 | 5
81037 2000 | 2
81037 2001 | 2
81037 2002 | 3
81037 2003 | 2
81037 2004 | 2
81037 2012 | 4
81037 2013 | 2
81037 2014 | 2
81037 2015 | 3
81037 2016 | 4
81037 2017 | 2
-----------------------

Comment