Hi there,

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
Using my example, I want to sum the variable "is_council_member" if they belong to the same "MUNICIPALITY" and the same "PARTY". In one municipality, there might be several parties. Therefore, the sum for rows with the same party and municipality should have the same number. The expected result of this conditional sum is the variable "num_council_party_municip"

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]
    }
I would really appreciate your help,

David