I have a dataset of municipalities which contains a number of vote brackets. For each municipality, there is a number (let's say 3) of voter brackets. Each bracket has a population and an average number of votes per person, but the bracket averages vary across municipalities. From this dataset we are trying to generate a new dataset which has one observation for every eligible voter in the country, where the individual’s number of votes is assumed to be equal to the average in their bracket. The purpose is to subsequently find how many of the total votes in the country that accrues to the top 10% of the population (when sorted by number of votes). My problem is finding a way to loop over all brackets in the dataset, since each municipality has their own unique brackets. What I need the program to to is to loop over both observations (municipalities) and two varlists (pop* and avg_votes*).
Here's how the data is formatted:
Code:
input str20 municipality pop1 pop2 pop3 avg_votes1 avg_votes2 avg_votes3 “Vasby” 40 32 54 8 16 56 “Karlstad” 60 15 12 9 15 54 “Alingsas” 15 75 12 8 18 60 end
Code:
input str20 municipality bracket_no votes “Vasby” 1 8 “Vasby” 1 8 “Vasby” 1 8 “Vasby” 1 8 // ...and so on for a total of 40 observations (that is, pop1 in observation 1) “Vasby” 2 16 “Vasby” 2 16 “Vasby” 2 16 “Vasby” 2 16 // ...and so on for a total of 32 observations (that is, pop2 in observation 1) “Vasby” 3 56 “Vasby” 3 56 “Vasby” 3 56 “Vasby” 3 56 // ...and so on for a total of 54 observations (that is, pop3 in observation 1) “Karlstad” 1 9 “Karlstad” 1 9 “Karlstad” 1 9 “Karlstad” 1 9 // ...and so on for a total of 60 observations (that is, pop1 in observation 2) //continuing trough the whole dataset end
Code:
frame create individuals municipality bracket_no votes // Creating a new dataset where the individual observations will be put local N = _N //Number of observations (municipalities) in the original dataset local no = 3 // Number of brackets in each municipality forvalues i = 1/`N' { //This loops trough the observations. forvalues j = 1/no { // This loops trough the brackets forvalues j = 1/pop`j' { // This loops trough the population in the bracket frame post individuals (municipality) (`j’) (avg_votes`j’) in `i' } } }
I have also tried to solve the same problem by creating a sort of histogram, creating a varlist with one variable for each possible number of votes (I have access to the number of votes held by the individual in the country that has the most votes). And then using a nested loop similiar to the one above to populate this histogram-ish varlist. With this method too, the central problem that I can't solve is how to construct a loop that executes a command for each individual in each of a number of brackets in each municipality.
I know that looping over observations is unconventional and I get the feeling that there is an entirely different way to go about this. Any suggestions?
0 Response to Nested loop over observations and varlist
Post a Comment