I have Brazilian electoral data with the names of candidates who ran for mayor from 2000 to 2016. The dataset contains information about the candidates' names (NOME_CANDIDATO), their party codes (party_code), the codes of the states (ibge_uf_code) and municipalities (ibge_mun_code) where they ran, and the number of votes they got (QTDE_VOTOS).

What I need: to create a variable called "incumbent_run_reelection" equal to 1 if the candidate who won an election at t-4 ran for reelection at election t, and zero otherwise (e.g., if John Doe won the election in 2000 and ran again in 2004, incumbency for him would be equal to 1). My goal is to know which municipalities have incumbent mayors running for reelection every election year.

The problem: I only have a string variable to work with. One possibility would be to encode "NOME_CANDIDATO", but the issue is that there are hundreds of repeated names in the dataset across municipalities.

Question: how can I create the "incumbent_run_reelection" variable with the "NOME_CANDIDATO" variable?

Thanks


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input int year byte ibge_uf_code str2 uf long ibge_mun_code str61 NOME_CANDIDATO byte party_code str13 party_label long QTDE_VOTOS
2000 11 "RO" 1100015 "NEREU JOSE KLOSINSKI"             13 "PT"    1655
2008 11 "RO" 1100015 "DANIEL DEINA"                     19 "PTN"   7930
2012 11 "RO" 1100015 "GIOVAN DAMO"                      45 "PSDB"  6552
2016 11 "RO" 1100015 "VALDOIR GOMES FERREIRA"           15 "PMDB"  3638
2016 11 "RO" 1100015 "CARLOS BORGES DA SILVA"           11 "PP"    9553
2004 11 "RO" 1100015 "VALDOIR GOMES FERREIRA"           12 "PDT"   7054
2000 11 "RO" 1100015 "MOISES JOSE RIBEIRO DE OLIVEIRA"  14 "PTB"   5133
2008 11 "RO" 1100015 "VALDOIR GOMES FERREIRA"           15 "PMDB"  6394
2012 11 "RO" 1100015 "VALDOIR GOMES FERREIRA"           15 "PMDB"  7465
2000 11 "RO" 1100015 "JOSE PEREIRA DE ASSIS"            15 "PMDB"  1075
2000 11 "RO" 1100015 "DARCILA TERESINHA CASSOL"         11 "PPB"   5452
2004 11 "RO" 1100015 "DARCILA TERESINHA CASSOL"         45 "PSDB"  6431
2004 11 "RO" 1100023 "ANTENOR KLOCH"                    43 "PV"    2601
2008 11 "RO" 1100023 "CONFUCIO AIRES MOURA"             15 "PMDB" 31329
2012 11 "RO" 1100023 "ADELINO ANGELO FOLLADOR"          25 "DEM"   8290
2000 11 "RO" 1100023 "ELISEU MULLER DE SIQUEIRA"        13 "PT"    3581
2004 11 "RO" 1100023 "CONFUCIO AIRES MOURA"             15 "PMDB" 20097
2000 11 "RO" 1100023 "ERNANDES SANTOS AMORIM"           11 "PPB"  16898
2000 11 "RO" 1100023 "MARCOS JUNIOR DOS SANTOS"         25 "PFL"   2419
2012 11 "RO" 1100023 "SAULO PIGNATON"                   14 "PTB"  12806
2008 11 "RO" 1100023 "LORIVAL RIBEIRO DE AMORIM"        27 "PSDC" 11122
2012 11 "RO" 1100023 "LORIVAL RIBEIRO DE AMORIM"        33 "PMN"  20371
2000 11 "RO" 1100023 "DONIZETTI JOSE"                   14 "PTB"   7209
2012 11 "RO" 1100023 "VALMIR FRANCISCO DOS SANTOS"      13 "PT"    4351
2008 11 "RO" 1100023 "ANTONIO EVERALDO JOCA"            50 "PSOL"   590
2004 11 "RO" 1100023 "DANIELA SANTANA AMORIM"           14 "PTB"  17736
2000 11 "RO" 1100023 "JOAO MARIA DE LIZ"                23 "PPS"   3392
2016 11 "RO" 1100023 "THIAGO LEITE FLORES PEREIRA"      15 "PMDB" 26808
2016 11 "RO" 1100023 "LORIVAL RIBEIRO DE AMORIM"        12 "PDT"  19333
2016 11 "RO" 1100031 "SILVENIO ANTONIO DE ALMEIDA"      15 "PMDB"  1868
2008 11 "RO" 1100031 "JOSE ROZARIO BARROSO"             22 "PR"    1458
2004 11 "RO" 1100031 "MOZAIR DIVINO DOS SANTOS"         13 "PT"    1723
2004 11 "RO" 1100031 "JOSE ROZARIO BARROSO"             12 "PDT"   1941
2000 11 "RO" 1100031 "JOSE ROZARIO BARROSO"             12 "PDT"   1275
2008 11 "RO" 1100031 "GILMAR DE CARLI"                  25 "DEM"   1814
2008 11 "RO" 1100031 "IZAEL DIAS MOREIRA"               14 "PTB"   2276
2016 11 "RO" 1100031 "GILMAR DE CARLI"                  43 "PV"    1858
2000 11 "RO" 1100031 "MOZAIR DIVINO DOS SANTOS"         13 "PT"     512
2000 11 "RO" 1100031 "MILTON MITSUO SAIKI"              25 "PFL"   2176
2012 11 "RO" 1100031 "IZAEL DIAS MOREIRA"               14 "PTB"   2889
2008 11 "RO" 1100031 "JOSE ROZARIO BARROSO"             22 "PR"    2229
2016 11 "RO" 1100049 "GLAUCIONE MARIA RODRIGUES NERI"   15 "PMDB" 19715
2000 11 "RO" 1100049 "NERI FIRIGOLO"                    13 "PT"    8151
2008 11 "RO" 1100049 "FRANCESCO VIALETTO"               13 "PT"   24601
2016 11 "RO" 1100049 "MARCO AURELIO BLAZ VASQUES"       25 "DEM"   7909
2016 11 "RO" 1100049 "ADAILTON ANTUNES FERREIRA"        10 "PRB"  12870
2012 11 "RO" 1100049 "FRANCESCO VIALETTO"               13 "PT"   21700
2016 11 "RO" 1100049 "JOSE COSTA"                       50 "PSOL"   217
2012 11 "RO" 1100049 "GLAUCIONE MARIA RODRIGUES"        27 "PSDC" 20133
2016 11 "RO" 1100049 "ACELINO LUIZ MARCON"              12 "PDT"   1604
2016 11 "RO" 1100049 "DIMAS GIACOMIN SELVATICI"         13 "PT"    1592
2004 11 "RO" 1100049 "SUELI ALVES ARAGAO"               15 "PMDB" 23035
2008 11 "RO" 1100049 "GLAUCIONE MARIA RODRIGUES NERI"   27 "PSDC" 14653
2000 11 "RO" 1100049 "SUELI ALVES ARAGAO"               15 "PMDB" 16686
2000 11 "RO" 1100049 "VILSON STECCA"                    23 "PPS"   7231
2004 11 "RO" 1100049 "DIVINO CARDOSO CAMPOS"            14 "PTB"  14900
2008 11 "RO" 1100049 "SILVERIO DOS SANTOS OLIVEIRA"     40 "PSB"   1243
2000 11 "RO" 1100056 "AMALIA CAMPOS MILANI E SILVA"     22 "PL"     499
2012 11 "RO" 1100056 "PEDRO JOSE ALVES SANCHES"         15 "PMDB"  4154
2016 11 "RO" 1100056 "KLEBER CALISTO DE SOUZA"          15 "PMDB"  3500
2008 11 "RO" 1100056 "KLEBER CALISTO DE SOUZA"          15 "PMDB"  7313
2000 11 "RO" 1100056 "MANOEL FRANCISCO DE ALMEIDA"      12 "PDT"   2124
2000 11 "RO" 1100056 "AIRTON GOMES"                     25 "PFL"   1440
2004 11 "RO" 1100056 "MANOEL FRANCISCO DE ALMEIDA"      45 "PSDB"  6924
2000 11 "RO" 1100056 "ISRAEL NEIVA DE CARVALHO"         15 "PMDB"  1764
2012 11 "RO" 1100056 "AIRTON GOMES"                     11 "PP"    6159
2000 11 "RO" 1100056 "MARIA FERREIRA"                   13 "PT"    1948
2008 11 "RO" 1100056 "VANESSA SIMOES D EFREITAS"        13 "PT"    2216
2004 11 "RO" 1100056 "VALDIR BENEDITO NAVARRO"          25 "PFL"   2895
2000 11 "RO" 1100056 "JOSE EUGENIO DE SOUZA"            14 "PTB"   2440
2016 11 "RO" 1100056 "AIRTON GOMES"                     11 "PP"    6421
2004 11 "RO" 1100064 "MARIO RODRIGUES LEITE"            15 "PMDB"  3859
2000 11 "RO" 1100064 "CERENEU JOAO NAUE"                12 "PDT"   4179
2000 11 "RO" 1100064 "EDSON LOPES DA SILVA"             45 "PSDB"  1430
2016 11 "RO" 1100064 "EDMILSON RODRIGUES DE ALMEIDA"    43 "PV"    3126
2016 11 "RO" 1100064 "JOSEMAR BEATTO"                   27 "PSDC"  2085
2012 11 "RO" 1100064 "APARECIDO DIAS DE OLIVEIRA"       13 "PT"    4518
2008 11 "RO" 1100064 "ANEDINO CARLOS PEREIRA JUNIOR"    11 "PP"    5699
2000 11 "RO" 1100064 "JOSE RODRIGUES DE SOUZA"          11 "PPB"   2203
2012 11 "RO" 1100064 "ANEDINO CARLOS PEREIRA JUNIOR"    11 "PP"    5976
2000 11 "RO" 1100064 "JUSSARA DIAS LEOPOLDO FERREIRA"   13 "PT"    2998
2000 11 "RO" 1100064 "JOUBERT ANTONIO MURACAMI"         40 "PSB"     68
2008 11 "RO" 1100064 "APARECIDO DIAS DE OLIVEIRA"       13 "PT"    4698
2016 11 "RO" 1100064 "JOSE RIBAMAR DE OLIVEIRA"         40 "PSB"   4519
2004 11 "RO" 1100064 "MIRIAN DONADON CAMPOS"            14 "PTB"   6741
2000 11 "RO" 1100072 "LEIDSON FERREIRA DE SOUSA"        15 "PMDB"  2429
2000 11 "RO" 1100072 "JOSE NUNES NETO"                  13 "PT"    1713
2012 11 "RO" 1100072 "DEOCLECIANO FERREIRA FILHO"       14 "PTB"   2100
2004 11 "RO" 1100072 "JOSUE DA SILVA LOPES"             45 "PSDB"   652
2016 11 "RO" 1100072 "LAERCIO MARCHINI"                 12 "PDT"   2206
2016 11 "RO" 1100072 "MARCELO CRISOSTOMO DO NASCIMENTO" 25 "DEM"    958
2016 11 "RO" 1100072 "LEANDRO TEIXEIRA VIEIRA"          40 "PSB"   1604
2004 11 "RO" 1100072 "SILVINO ALVES BOAVENTURA"         14 "PTB"   2269
2008 11 "RO" 1100072 "JOAO PEREIRA DE AGUIAR"           40 "PSB"    115
2004 11 "RO" 1100072 "JOUBERT ANTONIO MURACAMI"         40 "PSB"     24
2004 11 "RO" 1100072 "ADENIVAL MARCON"                  44 "PRP"    437
2012 11 "RO" 1100072 "RONELSON TERRES PORTELA"          23 "PPS"   1452
2012 11 "RO" 1100072 "VAGNER MEIRA TEIXEIRA"            13 "PT"    1343
2004 11 "RO" 1100072 "TEREZINHA APARECIDA ROSA"         13 "PT"    1285
2008 11 "RO" 1100072 "SELVINO ALVES BOAVENTURA"         14 "PTB"   2661
end