Dear Statalists,

I am fairly new to Stata and am facing some issues regarding nested foreach loops.

I would like to match whether a firm2 forms partnership with a firm1’s partner within 3years (n+1, n+2, and n+3) after the firm1’s partnership in year n. Using the attached hypothetical dataset as an an example, my goal is to generate variable (“matching”) by comparing the partnerships of AAA in firm2 with the partnerships of ZZZ, YYY, and XXX in firm1, and the partnerships of BBB in firm2 with the partnerships of WWW, and ZZZ in firm1, and so on.

* Example generated by -dataex-.
To install: ssc install dataex
clear
input int firm1_yr str3 firm1 str2 firm1_partner int firm2_yr str3 firm2 str2 firm2_partner byte matching
2010 "ZZZ" "ZA" 2010 "AAA" "ZA" 0
2010 "ZZZ" "ZB" 2011 "AAA" "ZB" 1
2011 "ZZZ" "ZC" 2009 "AAA" "ZC" 0
2012 "ZZZ" "ZD" 2013 "AAA" "YC" 1
2010 "YYY" "YA" 2016 "AAA" "YD" 0
2010 "YYY" "YB" 2014 "AAA" "XA" 1
2011 "YYY" "YC" 2013 "AAA" "XB" 1
2012 "YYY" "YD" 2011 "AAA" "XG" 0
2013 "YYY" "YE" 2015 "AAA" "XH" 1
2013 "YYY" "YF" 2012 "AAA" "AA" 0
2011 "XXX" "XA" 2013 "AAA" "AB" 0
2011 "XXX" "XB" 2014 "AAA" "AC" 0
2011 "XXX" "XC" 2015 "AAA" "AD" 0
2011 "XXX" "XD" . "AAA" "." 0
2012 "XXX" "XE" . "AAA" "." 0
2012 "XXX" "XF" . "AAA" "." 0
2012 "XXX" "XG" . "AAA" "." 0
2013 "XXX" "XH" . "AAA" "." 0
2012 "WWW" "WA" 2012 "BBB" "BA" 0
2012 "WWW" "WB" 2012 "BBB" "BC" 0
2013 "WWW" "WC" 2013 "BBB" "BD" 0
2014 "WWW" "WD" 2014 "BBB" "BE" 0
2013 "ZZZ" "ZA" 2014 "BBB" "BF" 0
2013 "ZZZ" "ZB" 2015 "BBB" "BG" 0
2014 "ZZZ" "ZC" 2015 "BBB" "BH" 0
2014 "ZZZ" "ZD" 2016 "BBB" "WC" 1
. "." "" 2016 "BBB" "WD" 1
. "." "" 2016 "BBB" "ZC" 1
. "." "" 2018 "BBB" "ZD" 0
2014 "VVV" "VA" 2011 "CCC" "CA" 0
2014 "VVV" "VB" 2011 "CCC" "CB" 0
2013 "UUU" "UA" 2013 "CCC" "CD" 0
2015 "UUU" "UB" 2014 "CCC" "CE" 0
2015 "UUU" "UC" 2015 "CCC" "CF" 0
2015 "UUU" "UD" 2015 "CCC" "VB" 1
2016 "UUU" "UE" 2015 "CCC" "UB" 0
2013 "WWW" "WC" 2017 "CCC" "UC" 1
2014 "WWW" "WF" 2018 "CCC" "WF" 0
End

Ideally, the value of the variable “matching” should be like in the attached dataset (I manually coded this variable to clearly present what I want). It should be 1 if a firm2’s partnership is formed with a firm1’s partner and such a partnership is formed within 3years of a firm1’s partnership, otherwise 0. For example, AAA ‘s partnership with ZB in 2011 is coded 1 because ZB formed partnership with ZZZ in firm1 in year 2010, while AAA ‘s partnership with ZA (YD) in 2010 (2016) is coded 0 because ZA (YD) formed partnership with ZZZ (YYY) in firm1 in year 2010 (2012).

Because it needs multiple comparisons (firm1, firm2, firm1’s partner, firm2’s partner, and years of firm1 and firm2’s partnership), I think “foreach nested loop” is appropriate. So far, all I could is to develop the simple code below but even it does not work.

gen matching1=0
levelsof firm1_partner, local(firm1_prt)
levelsof firm2_partner, local(firm2_prt)
foreach i of local firm2_prt {
foreach j of firm1_prt{
recode matching1 (0=1) if "`i'"=="`j'" & "`j'"=="`i'"
}
}

Stata is returning

invalid syntax
r(198);
end of do-file

Currently, I am using STATA/SE 14.2

I know what I have done so far is not enough but can't figure out how to improve it.
Any help is highly appreciated! Thank you all in advance,

Kevin