Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating a reelection variable from panel data based on the name of candidates

    Hi all. I have panel data containing the variables: "year", municipal ID code ("ibge_mun_code"), the names of Brazilian candidates for mayor ("nome_cand"), their votes ("votes"), and the electoral result for each candidate, variable "elected" (i.e. elected or non-elected) over five four-year elections (i.e., 2000-2004-2008-2012-2016), N = 11,339. I need to create a variable called "ran_reelection", meaning whether the winner in one election (let's say 2004) ran for office again in 2008. I haven't been able to automate the creation of this variable. Does someone know how to do it? Thank you.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year long ibge_mun_code str61 nome_cand long votes float elected
    2000 3106200 "CELIO DE CASTRO"                                                518600 1
    2000 3106200 "JOAO LEITE DA SILVA NETO"                                 372257 0
    2000 3106200 "MARIA ELVIRA SALLES FERREIRA"                       206468 0
    2000 3106200 "GENTIL CIRILO DA ANUNCIACAO JUNIOR"              4086 0
    2000 3106200 "ANTONIO FRANCISCO DE OLIVEIRA FILHO"          21064 0
    2000 3106200 "GLYCON TERRA PINTO"                                            47368 0
    2000 3106200 "JOSE EUSTAQUIO GOMES DE FARIA"                       1584 0
    2000 3106200 "ANTONIO FELICIANO DOS SANTOS"                       18521 0
    2000 3106200 "FRANCISCO SIMOES DE AGUIAR"                             1117 0
    2004 3106200 "VANESSA PORTUGAL BARBOSA"                            16854 0
    2004 3106200 "JOAO LEITE DA SILVA NETO."                                 290194 0
    2004 3106200 "ROBERTO FRANCISCO PEREIRA"                           16927 0
    2004 3106200 "FERNANDO DAMATA PIMENTEL"                           872601 1
    2004 3106200 "ROBERTO LUCIO ROCHA BRANT"                           77487 0
    2008 3106200 "MARIA DO SOCORRO JO MORAES"                       111060 0
    2008 3106200 "JORGE ANDRE SOUZA PERIQUITO"                          4769 0
    2008 3106200 "GUSTAVO DE CUNHA PEREIRA VALADARES"        18974 0
    2008 3106200 "ANDRE ANTONIO ALVES"                                           4428 0
    2008 3106200 "SERGIO MIRANDA DE MATOS BRITO"                     42812 0
    2008 3106200 "MARCIO ARAUJO DE LACERDA"                            549131 1
    2008 3106200 "PEDRO PAULO DE ABREU PINHEIRO"                      1593 0
    2008 3106200 "LEONARDO LEMOS BARROS QUINTAO"               519787 0
    2008 3106200 "VANESSA PORTUGAL BARBOSA"                              7123 0
    2012 3106200 "PEDRO PAULO DE ABREU PINHEIRO"                        782 0
    2012 3106200 "MARCIO ARAUJO DE LACERDA"                            676215 1
    2012 3106200 "ALFREDO FLISTER"                                                     4691 0
    2012 3106200 "MARIA DA CONSOLACAO ROCHA"                          54530 0
    2012 3106200 "PATRUS ANANIAS DE SOUSA"                               523645 0
    2012 3106200 "VANESSA PORTUGAL BARBOSA"                            19908 0
    2012 3106200 "TADEU MARTINS SOARES"                                         3728 0
    2016 3106200 "LUIS HENRIQUE DE OLIVEIRA RESENDE"              38682 0
    2016 3106200 "ALEXANDRE KALIL"                                                  314845 0
    2016 3106200 "MARCELO HENRIQUE TEIXEIRA DIAS"                    32121 0
    2016 3106200 "REGINALDO LAZARO DE OLIVEIRA LOPES"            86234 0
    2016 3106200 "DELIO DE JESUS MALHEIROS"                                 64606 0
    2016 3106200 "JOAO LEITE DA SILVA NETO"                                   395952 1
    2016 3106200 "WASHINGTON FERNANDO RODRIGUES"                34088 0
    2016 3106200 "VANESSA PORTUGAL BARBOSA"                               5256 0
    2016 3106200 "RODRIGO OTAVIO SOARES PACHECO"                 118772 0
    2016 3106200 "EROS FERREIRA BIONDINI"                                      46270 0
    2016 3106200 "MARIA DA CONSOLACAO ROCHA"                           48715 0
    end

  • #2
    Code:
    bys nome_cand(year): gen beenelected= sum(elected)
    replace beenelected= 0 if elected==1 & beenelected==1
    gen wanted= beenelected>0
    Note that you have to clean up your "nome_cand" variable (which as far as I can see serves as your id variable) before proceeding to generate your variable. For example, the third observation for the following candidate is not linked to his other observations due to the presence of a period. If not sorted out, this will not lead to the desired result.

    Code:
    . list nome_cand in 14/16
    
         +---------------------------+
         |                 nome_cand |
         |---------------------------|
     14. |  JOAO LEITE DA SILVA NETO |
     15. |  JOAO LEITE DA SILVA NETO |
     16. | JOAO LEITE DA SILVA NETO. |
         +---------------------------+
    Last edited by Andrew Musau; 11 Sep 2019, 03:15.

    Comment


    • #3
      Thanks for the reply Andrew. I think the code is almost there, but not there yet. My reelection variable has to show whether the "winner" (that's important) in one election ran for office again in the next election. Your code retrives all the times a candidate ran, whether the candidate lost or won. For instance, JOAO LEITE DA SILVA NETO is listed three times, including two times he was defeated. The code has to show whether the winner at election t ran at election t+1. An example of a name that would come up with the correct code would be MARCIO ARAUJO DE LACERDA. He ran (and won) in 2008 and ran again in 2012.

      Comment


      • #4
        Code:
        encode nome_cand, gen(id)
        xtset id year, delta(4)
        gen wanted= l1.elected==1

        Comment


        • #5
          Thanks Andrew. I got two error messages, one for "xtset id year, delta(4)" (repeated time values within panel) and one for "gen wanted= l1.elected==1" (time variable not set).

          Comment


          • #6
            It means you have duplicate observations. You can inspect these using

            Code:
            duplicates tag id year, gen(dup)
            list if dup
            If these are just duplicates (no extra information), then

            Code:
            duplicates drop id year, force
            xtset id year, delta(4)
            gen wanted= l1.elected==1

            Comment


            • #7
              Thank you very much Andrew. This time it worked.

              Comment

              Working...
              X