Hello everyone! I am trying to do something which, upon reading manuals and forum entries, seems perhaps unconventional. I am starting to get the impression that I am going about the problem in a convoluted way and that there might be a completely different and more clever way to do it. Here goes:

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
Here is how I imagine the final dataset of individuals looking like:

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
And here is what I’ve tried to do:

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'
        }

    }
    
}
Now the above gives me "invalid syntax". I think I am misusing the local variables and/or loops.

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?