Dear Statalist, I am wondering if you can help me to count the number of times a given attribute is reapeted within a group. As you wil see in the dataex, I have cited patents (cited_appln_id) that are cited by other patents (citing_appln_id) in different years. I have count the number of times a given patent is cited by another patent in the same year (considering first that the citing firm is not the same as the cited firm in a given row; but also in another row within the group). See the comand below.

However, I need to do the same but 5 years after the current citation. For instance, for the first row which is a citation made in 2010, I need to count how many times the same patent (cited_appln_id) is cited 5 years after, so, in 2015. And put that number of repetitions in the row from 2010. In fact, there is two other citing patent in 2015 within that group (cited_appln_id==118). However, non of these citing patents should come from the same citing firm in 2010 or the cited firm. Thus, for this first observation in 2010, there is only one patent that fulfill that requirement in the group (a patent 5 years after that comes from a different citing and cited firm), and should count as 1 in the first row.

Can you help me to build such a variable?

Code:
egen count_t0 = count(citing_appln_id) if citing_firm_id!=. & cited_firm_id!=. & (citing_firm_id!=cited_firm_id), by(cited_appln_id citing_year)
bys cited_appln_id citing_firm_id citing_year: egen count4 = count(citing_firm_id) if !missing(citing_firm_id,cited_firm_id)  
replace count_t0 = count_t0 - count4
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long citing_firm_id strL citing_firm_name float citing_year long(citing_appln_id cited_firm_id) strL cited_firm_name long cited_appln_id
 441693 "PANASONIC INTELLECTUAL PROPERTY MANAGEMENT CO LTD" 2010 267381063  193029 "MATSUSHITA ELECT WORKS LTD"        118
1977789 "LSIS CO LTD"                                       2012 337710889  193029 "MATSUSHITA ELECT WORKS LTD"        118
2236495 "OMRON CORP"                                        2013 337112549  193029 "MATSUSHITA ELECT WORKS LTD"        118
1977789 "LSIS CO LTD"                                       2015 422756603  193029 "MATSUSHITA ELECT WORKS LTD"        118
 441693 "PANASONIC INTELLECTUAL PROPERTY MANAGEMENT CO LTD" 2015 423854029  193029 "MATSUSHITA ELECT WORKS LTD"        118
1977789 "LSIS CO LTD"                                       2016 441031936  193029 "MATSUSHITA ELECT WORKS LTD"        118
 441693 "PANASONIC INTELLECTUAL PROPERTY MANAGEMENT CO LTD" 2016 451986450  193029 "MATSUSHITA ELECT WORKS LTD"        118
 654184 "DELTA ELECT INC"                                   2018 474491980  193029 "MATSUSHITA ELECT WORKS LTD"        118
3330322 "WILKHAHN WILKENING HAHNE GMBH CO KG"               2012 341573942 3330322 "WILKHAHN WILKENING HAHNE GMBH CO"  159
      . ""                                                  2012 353630717 3330322 "WILKHAHN WILKENING HAHNE GMBH CO"  159
 735529 "VITRA PATENTE AG"                                  2015 378088313 3330322 "WILKHAHN WILKENING HAHNE GMBH CO"  159
3330322 "WILKHAHN WILKENING HAHNE GMBH CO"                  2015 437771488 3330322 "WILKHAHN WILKENING HAHNE GMBH CO"  159
3330322 "WILKHAHN WILKENING HAHNE GMBH CO KG"               2016 443272867 3330322 "WILKHAHN WILKENING HAHNE GMBH CO"  159
1396276 "INT TOBACCO MACHINERY POLAND SP Z OO"              2017 445453181 1193132 "HAUNI MASCHINENBAU AG"             374
1235329 "HAUNI MASCHINENBAU GMBH"                           2017 477107631 1193132 "HAUNI MASCHINENBAU AG"             374
1686948 "KOMORI CORP"                                       2001  15723135 1266878 "HEIDELBERG HARRIS SA"              748
1110775 "GOSS INT AMERICAS INC"                             2001  15725727 1266878 "HEIDELBERG HARRIS SA"              748
1686948 "KOMORI CORP"                                       2001  15811044 1266878 "HEIDELBERG HARRIS SA"              748
1611455 "KOENIG & BAUER AG"                                 2005  16207716 1266878 "HEIDELBERG HARRIS SA"              748
1611455 "KOENIG & BAUER AG"                                 2008  16398409 1204378 "HEIDELBERGER DRUCKMASCHINEN AG"    748
1611455 "KOENIG & BAUER AG"                                 2008  16398442 1266878 "HEIDELBERG HARRIS SA"              748
1850282 "MANROLAND AG"                                      2009  57153984 1266878 "HEIDELBERG HARRIS SA"              748
1850282 "MANROLAND AG"                                      2009  57232896 1266878 "HEIDELBERG HARRIS SA"              748
1850282 "MANROLAND AG"                                      2009  57232901 1266878 "HEIDELBERG HARRIS SA"              748
1611455 "KOENIG & BAUER AG"                                 2012 333778409 1266878 "HEIDELBERG HARRIS SA"              748
1611455 "KOENIG & BAUER AG"                                 2012 333778439 1266878 "HEIDELBERG HARRIS SA"              748
2709504 "SIEMENS AG"                                        2010  56777754 2709504 "SIEMENS AG"                       1028
2709504 "SIEMENS AG"                                        2010  57020735 2709504 "SIEMENS AG"                       1028
2709504 "SIEMENS AG"                                        2010  57049066 2709504 "SIEMENS AG"                       1028
2709504 "SIEMENS AG"                                        2012 338475772 2709504 "SIEMENS AG"                       1028
1032537 "GENERAL ELECT CO"                                  2013 405371049 2709504 "SIEMENS AG"                       1028
1032537 "GENERAL ELECT CO"                                  2013 405968754 2709504 "SIEMENS AG"                       1028
2709504 "SIEMENS AG"                                        2016 420254450 2709504 "SIEMENS AG"                       1028
2709504 "SIEMENS AG"                                        2017 444438060 2709504 "SIEMENS AG"                       1028
4545720 "MITSUBISHI POWER LTD"                              2019 496108376 2709504 "SIEMENS AG"                       1028
1982247 "MITSUBISHI HITACHI POWER SYSTEMS LTD"              2019 496108861 2709504 "SIEMENS AG"                       1028
 705757 "DOOSAN HEAVY IND & CONSTRUCTION CO LTD"            2019 499582923 2709504 "SIEMENS AG"                       1028
end