Hello! I have a dataset containing voters as observations. It includes parties they voted for (par) and elections in which they voted (elc). From total number of votes each party gained (votNUM) and total number of voters per election I'd like to create a variable that indicates how many voters voted for the same party in the same election on a scale from 0 to 1 (vot). I need to do this to compensate for different sample sizes of elections. Returning the number of observations per election works perfectly when I do it manually, but looping the same process only returns the total number of observations in spite of the "if elc == `elc'" statement.
What's wrong with the loop?
What's wrong with the loop?
bys par: gen votNUM = _N
gen vot = .
foreach elc in elc {
quietly sum votNUM if elc == `elc'
quietly replace vot = (votNUM/r(N)) if elc == `elc'
}
gen vot = .
foreach elc in elc {
quietly sum votNUM if elc == `elc'
quietly replace vot = (votNUM/r(N)) if elc == `elc'
}
Comment