Q:

Count variable for unbalanced panel data, repeating the last value conditioned on a binary variable

I am having a hard time generating a count variable count = _n for  unbalanced panel data with the following conditions in Stata:  
Starting counting (from _n) every time the binary variable is equal 1, if it changes from 1 to 0, then count repeats the last value until the binary changes to 1 again (in this case keep counting from where it stopped). 
I think it is much easier to show my problem giving an example of my dataset: 
year    country  binary  count[variable that I want to create]
1959        1       0       0
1960        1       0       0
1961        1       1       1
1962        1       1       2
1963        1       1       3
1964        1       0       3
1965        1       0       3
1966        1       .       .
1967        1       1       4
1959        2       0       0
1960        2       0       0
1961        2       0       0
1962        2       1       1
1963        2       1       2
1964        2       .       .
1965        2       .       .
1966        2       0       2
1967        2       0       2
1968        2       1       3
1969        2       1       4
1970        2       0       4
1959        3       1       1
1960        3       1       2
1961        3       1       3
1962        3       0       3
1963        3       .       .
1964        3       1       4
1965        3       .       .
1966        3       0       4

In the above example, for country = 1, my new variable count starts counting (from _n) at the year 1961 (because for this year, the binary variable is equal to 1), then it counts (from _n) until binary = 0 again (keeping the same last count number). 
Missing values are kept as missing values in the new variable. 

A:

With the data you show, the following creates ncount which matches your values of count.
bysort country (year): generate ncount = sum(binary)
replace ncount = . if missing(binary)
list, sepby(country)

