I posted this question in a response to a topic earlier, but I am not sure whether that is the right place.

In my research I jumped into another challenging variable that I think needs extensive programming. I have come quite a way, but I am not sure how to formally put in STATA. Here is an example of the data:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double director_detail_id str12 CUSIP double year float hasfemale
 1 "922944103" 2007 1
 1 "344849104" 2008 1
 1 "344849104" 2009 1
 1 "344849104" 2010 1
 1 "344849104" 2011 1
 1 "344849104" 2012 1
 2 "638620104" 2007 0
 2 "638620104" 2008 0
 2 "638620104" 2009 0
 2 "638620104" 2010 0
 3 "28176E108" 2007 1
 4 "053611109" 2007 1
 4 "053611109" 2008 1
 4 "053611109" 2009 1
 4 "053611109" 2010 1
 4 "053611109" 2011 1
 4 "053611109" 2012 1
 5 "053611109" 2007 1
 5 "053611109" 2008 1
 5 "053611109" 2009 1
 6 "17163B102" 2007 0
 6 "17163B102" 2008 0
 8 "210371100" 2007 1
 8 "210371100" 2008 1
 9 "017361106" 2007 1
 9 "017361106" 2008 1
 9 "631103108" 2008 1
 9 "631103108" 2009 1
 9 "017361106" 2009 1
 9 "017361106" 2010 1
 9 "631103108" 2010 1
 9 "631103108" 2011 1
12 "170388102" 2007 1
12 "141487AC2" 2007 1
12 "78387G103" 2007 1
12 "14149Y108" 2008 1
12 "00206R102" 2008 1
12 "00206R102" 2009 1
12 "00206R102" 2010 1
12 "00206R102" 2011 1
12 "00206R102" 2012 1
12 "00206R102" 2013 1
12 "00206R102" 2014 1
12 "00206R102" 2015 1
13 "141487AC2" 2007 1
13 "025816109" 2007 1
13 "025816109" 2008 1
13 "988498101" 2008 1
13 "025816109" 2009 1
13 "655664100" 2009 1
13 "988498101" 2009 1
13 "655664100" 2010 1
13 "025816109" 2010 1
13 "988498101" 2010 1
13 "655664100" 2011 1
13 "025816109" 2011 1
13 "988498101" 2011 1
13 "655664100" 2012 1
13 "025816109" 2012 1
13 "988498101" 2012 1
13 "025816109" 2013 1
13 "655664100" 2013 1
13 "988498101" 2013 1
13 "988498101" 2014 1
13 "655664100" 2014 1
13 "025816109" 2014 1
13 "025816109" 2015 1
13 "655664100" 2015 1
13 "988498101" 2015 1
13 "655664100" 2016 1
13 "988498101" 2016 1
13 "025816109" 2016 1
13 "988498101" 2017 1
13 "025816109" 2017 1
13 "655664100" 2017 1
13 "988498101" 2018 1
13 "988498101" 2019 1
14 "925524AC4" 2007 1
14 "06605LAF6" 2007 1
14 "100599208" 2007 1
14 "060505104" 2008 1
14 "124857202" 2008 1
14 "124857202" 2009 1
14 "060505104" 2009 1
14 "67019E107" 2009 1
14 "124857202" 2010 1
14 "67019E107" 2010 1
14 "060505104" 2010 1
14 "124857103" 2011 1
14 "060505104" 2011 1
14 "124857103" 2012 1
14 "060505104" 2012 1
14 "664397106" 2012 1
14 "124857103" 2013 1
14 "664397106" 2013 1
14 "060505104" 2013 1
14 "30040W108" 2014 1
14 "124857103" 2014 1
14 "060505104" 2014 1
14 "30040W108" 2015 1
end
What I want to achieve is a dummy that is 1 if a director (director_detail_id) is, for a given year, also in another firm (CUSIP) in which there is a female (hasfemale==1). I assume I would have to create some kind of loop that goes looking if the director code can be found in another CUSIP in the same year for which the dummy hasfemale==1. So for example in this case suppose director #1 was also in firm #344849104 for the year 2007. Then the dummy would have to equal 1 for the director in that firm in that year, as in that year he is also a director in firm #922944103 in which in 2007 there is female on the board.

I tried on my own to work around this problem via dummies. In which connections counts how often a director is in a firm in which there is a woman for a given year.
Code:
 (i) egen connections=count(director_detail_id) if hasfemale==1, by(director_detail_id year)

(ii) replace connections=0 if connections==.

(iii) replace connections=0 if connections==1 & hasfemale==1

(iv) replace connections=1 if connections>1
But I am not sure if this method is watertight. The reasoning behind (iii) is that if a director has only 1 connection and there is a female in his own firm in that year, there is not a female in the other firm he is in, hence the dummy should be 0.