Hi to all,
I have a problem managing a dataset on some electoral outcomes. I have a list of elections in different Municipalities for different years. Not all the Municipalities have the same number of elections (with a minimum of 2 to a maximum of 4) and may occur in different years. For each election I have the name of the candidate, his vote share and the electoral lists that support him. Again, candidates may be supported by a different number of lists, with a minimum of 1 and a maximum of 8. I would like to create a variable that for each election would tell me in which position the candidate came, depending on his votes.
I have created a unique identifier for each election (combining Municipality name and Year) and I've used the function "by election_id: egen rank = rank(candidate_votes),track" but when there are candidates with several lists, the code counts multiple observations, not allowing me to calculate the exact position of the candidate and I have to maintain the different electoral lists to make an analysis of the parties supporting each candidate.
To make a pratical example I consider here the elections of Abano Terme in the 2011.
rank election_id candidate_votes
22 Abano Terme2011 327
21 Abano Terme2011 381
20 Abano Terme2011 498
19 Abano Terme2011 606
16 Abano Terme2011 1355
16 Abano Terme2011 1355
16 Abano Terme2011 1355
11 Abano Terme2011 1900
11 Abano Terme2011 1900
11 Abano Terme2011 1900
11 Abano Terme2011 1900
11 Abano Terme2011 1900
6 Abano Terme2011 2820
6 Abano Terme2011 2820
6 Abano Terme2011 2820
6 Abano Terme2011 2820
6 Abano Terme2011 2820
1 Abano Terme2011 3442
1 Abano Terme2011 3442
1 Abano Terme2011 3442
1 Abano Terme2011 3442
1 Abano Terme2011 3442
I want instead that my variable rank is in continuous descending order and considers candidates equal even if supported by different lists. It should therefore take the values:
rank election_id candidate_votes
8 Abano Terme2011 327
7 Abano Terme2011 381
6 Abano Terme2011 498
5 Abano Terme2011 606
4 Abano Terme2011 1355
4 Abano Terme2011 1355
4 Abano Terme2011 1355
3 Abano Terme2011 1900
3 Abano Terme2011 1900
3 Abano Terme2011 1900
3 Abano Terme2011 1900
3 Abano Terme2011 1900
2 Abano Terme2011 2820
2 Abano Terme2011 2820
2 Abano Terme2011 2820
2 Abano Terme2011 2820
2 Abano Terme2011 2820
1 Abano Terme2011 3442
1 Abano Terme2011 3442
1 Abano Terme2011 3442
1 Abano Terme2011 3442
1 Abano Terme2011 3442
and this repeated for every single election. Thanks in advance for the help.
I have a problem managing a dataset on some electoral outcomes. I have a list of elections in different Municipalities for different years. Not all the Municipalities have the same number of elections (with a minimum of 2 to a maximum of 4) and may occur in different years. For each election I have the name of the candidate, his vote share and the electoral lists that support him. Again, candidates may be supported by a different number of lists, with a minimum of 1 and a maximum of 8. I would like to create a variable that for each election would tell me in which position the candidate came, depending on his votes.
I have created a unique identifier for each election (combining Municipality name and Year) and I've used the function "by election_id: egen rank = rank(candidate_votes),track" but when there are candidates with several lists, the code counts multiple observations, not allowing me to calculate the exact position of the candidate and I have to maintain the different electoral lists to make an analysis of the parties supporting each candidate.
To make a pratical example I consider here the elections of Abano Terme in the 2011.
rank election_id candidate_votes
22 Abano Terme2011 327
21 Abano Terme2011 381
20 Abano Terme2011 498
19 Abano Terme2011 606
16 Abano Terme2011 1355
16 Abano Terme2011 1355
16 Abano Terme2011 1355
11 Abano Terme2011 1900
11 Abano Terme2011 1900
11 Abano Terme2011 1900
11 Abano Terme2011 1900
11 Abano Terme2011 1900
6 Abano Terme2011 2820
6 Abano Terme2011 2820
6 Abano Terme2011 2820
6 Abano Terme2011 2820
6 Abano Terme2011 2820
1 Abano Terme2011 3442
1 Abano Terme2011 3442
1 Abano Terme2011 3442
1 Abano Terme2011 3442
1 Abano Terme2011 3442
I want instead that my variable rank is in continuous descending order and considers candidates equal even if supported by different lists. It should therefore take the values:
rank election_id candidate_votes
8 Abano Terme2011 327
7 Abano Terme2011 381
6 Abano Terme2011 498
5 Abano Terme2011 606
4 Abano Terme2011 1355
4 Abano Terme2011 1355
4 Abano Terme2011 1355
3 Abano Terme2011 1900
3 Abano Terme2011 1900
3 Abano Terme2011 1900
3 Abano Terme2011 1900
3 Abano Terme2011 1900
2 Abano Terme2011 2820
2 Abano Terme2011 2820
2 Abano Terme2011 2820
2 Abano Terme2011 2820
2 Abano Terme2011 2820
1 Abano Terme2011 3442
1 Abano Terme2011 3442
1 Abano Terme2011 3442
1 Abano Terme2011 3442
1 Abano Terme2011 3442
and this repeated for every single election. Thanks in advance for the help.
Comment