I am learning STATA and currently having the following situation: I want to sum a binary variable that counts the number of council members. I want to sum council members that belong to the same party and the same municipality. The variables municipality and party are string variables. To illustrate my case, this an example of my dataset:
municipality | Party | is_council_member | num_council_party_municip |
BRA | CONS | 1 | 2 |
COL | LIB | 0 | 1 |
BRA | CONS | 1 | 2 |
COL | LIB | 1 | 1 |
BRA | LIB | 1 | 1 |
COL | CONS | 1 | 1 |
I tried (unsuccesfully) to use a loop to compare each pair of observations n and n-1 and determine if their parties and municipalities were the same for summing the variable of interest:
Code:
gen num_council_party_mun =. foreach v of var municipality { replace num_council_party_mun = sum(is_council_member) if municipality[_n]==municipality[_n-1]| party1[_n]==party1[_n-1] }
David
0 Response to Summing a variable based on the values of other categorical variables
Post a Comment