Announcement

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

  • Renaming Variables from string in other variabel

    Hi there,

    I have a peculiar problem. We are working with panachage voting from Swiss elections. Our dataset has this kind of peculiar structure (given by how the statistical office provides them in excel):
    Candidate name Party name List number Votes from List 1 Votes from List 2 ....
    A B SP List 1 1 2390 1199
    C D SP List 2 2 1282 2478
    The idea is that we have an entry per candidate from every list (more than 3000 total). I want to calculate how many votes each candidate got from his list AND lists from the same party.

    What I am thinking of is to rename the "Votes from list 1" variable to "SP List 1" (note that "SP List 1" applies to several indidivuals in the dataset). Is there a way to automatically do this?


    The reason I'd like to autoamtically do this is that we wiill eventually have 20 regions with different list names and about 7 elections, so doing this by hand would likely be outside of the scope of our work. However, this can be ignored for the solution, since the data is in such a format that I can reasonably do a possible automation code for each region.

    I should mention that this is for class work in political science. I hope my explanation of the problem is understandable

    Thank you very much for your help!

  • #2
    If I understood right, you may start by fiddling with - reshape long - command. With regards to homeworks, please read the FAQ advice on this.
    Best regards,

    Marcos

    Comment


    • #3
      HI Marcos, Thanks for your reply! I'm not sure this does quite what I want - I want to change variable names (if at all possibile)

      Comment


      • #4
        Neither "Votes from list 1" nor "SP List 1" are valid variable names in Stata. Why do you want to rename those variables in the first place? How is this going to help you solve the underlying problem?

        Best
        Daniel
        Last edited by daniel klein; 10 May 2019, 08:52.

        Comment


        • #5
          So my problem is that I am adding together vote numbrs from different lists. But sometimes, parties run with two or more lists (up to five). Now, if I had the party names as the variable name (instead of list number which is randomly assigned), I could automate this - basically calculate the number of votes from parties with the same designation as the observation. Here's my data structure:

          Code:
          input str44 id byte Nr str134 Bezeichnung int Liste10 long Liste11 int Liste12 long Liste13 int(Liste14 Liste15)
          "SchulerHubert"              10 "SP"                                                                         1779  18 12  25  0   30
          "Bürgi DellspergerChristina" 10 "SP"                                                                         1315  31  7  17  4   11
          "SivaganesanRupan"           10 "SP"                                                                          465  14 14  11  0   12
          ""                            . ""                                                                            148   .  .   .  .    .
          "Hutter ElsenerSimone"       11 "SP Frauen"                                                                   142 167  3  18  1   10
          "HugMalaika"                 11 "SP Frauen"                                                                    81  90  8  18  0    7
          "Mäder BeglingerAnne"        11 "SP Frauen"                                                                    54  87  9   3  1    3
          ""                            . ""                                                                              .  12  .   .  .    .
          "SpeschaAnna"                12 "SP Juso"                                                                      65  15 99  14  1    5
          "KrasniciDenis"              12 "SP Juso"                                                                      47  12 83  18  1    2
          "KoepfliVirginia"            12 "SP Juso"                                                                      15   2 27   2  1    1
          ""                            . ""                                                                              .   . 10   .  .    .
          "DzaferiZari"                13 "SP Männer"                                                                   346  34 23 518  8   39
          "FreimannFabian"             13 "SP Männer"                                                                    25   3  4  89  0    4
          "SuterGuido"                 13 "SP Männer"                                                                    76   8  8 122  0    2
          ""                            . ""                                                                              .   .  .  72  .    .
          "SönmezSehriban"             14 "SP MigrantInnen"                                                              11   1  0   0 10    2
          "CoralicFadila"              14 "SP MigrantInnen"                                                              12   3  0   4 22    0
          "SimsekDeniz"                14 "SP MigrantInnen"                                                               3   1  1   2 11    1
          ""                            . ""                                                                              .   .  .   .  4    .
          "AeschiThomas"               15 "SVP Schweizerische Volkspartei"                                               33   2  3   4  1 7888
          "WernerThomas"               15 "SVP Schweizerische Volkspartei"                                                6   1  2   0  0 2103
          "VilligerThomas"             15 "SVP Schweizerische Volkspartei"                                                3   3  1   1  0  960
          "Bezeichnung" = Party name, "Nr" = List number, which always corresponds with the list name variable.

          So what I want in the end is to calculate how many votes each candidate got from their own party (in this case, for someone from list 10, from lists 10 - 14).

          The issue I have is that I can't do that automatically because I don't know how to tell stata that for A, I want it to count votes from lists 10,11, 12,13 + 14 *automatically* since the variable names are only labelled with the list number. So what I thought I might be able to do is to transfer the "Bezeichnung" (which means list name) from that column to become the variable name, dependin on the list

          So what I *think* I want to do is automatically make the string "SP" into the variable name for Liste10" and the string "SP Frauen" to the variable name for "Liste11", et cetera

          If I had that, I could tell stata to add up all the numbers for each candidate for the variables whose name contains "SP".

          Since I have this same problem about 200 times, i'd rather not do this by hand if in any way possible (and rather concentracte on the nicer problems that still need some thought with our regression analysis). Do you think this is at all possibile?
          Last edited by Dominik Fitze; 10 May 2019, 09:33.

          Comment


          • #6
            https://www.statalist.org/forums/help#stata explains how to give example data. Please read and act on it.

            Comment


            • #7
              Originally posted by Nick Cox View Post
              https://www.statalist.org/forums/help#stata explains how to give example data. Please read and act on it.
              thank you very much! I updated my post with a snapshot of my data.

              Comment

              Working...
              X