Could someone please take a look at a line of code and tell me where I am going wrong. Of my 3,000 US counties, I have 50 counties with a county police agency (agencysamptype == 700) and a county sheriff's agency (agencysamptype == 1) that have the same value of POPESTIMATE2010, which equals the agency's population served/policed in 2010. Other counties have a county police department and a county sheriff's office but with different population served values. I am trying to generate an indicator equal to 1 when a county has a county police department and a county sheriff's office with the same value of POPESTIMATE2010. I tried two different codes based on things I read online, but neither are working. Once I create the indicator, I would like to apply the result to every observation in the county, even agencies that are not county police departments or sheriff's offices. Any help with this code would also be much appreciated.

Code 1:

by fstate fcounty, sort: gen samepop = cond(POPESTIMATE2010/(agencysamptype == 1) == POPESTIMATE2010/(agencysamptype == 700), 1, 0)

Based on: https://www.stata.com/support/faqs/d...ng-properties/

Code 2:

sort fstate fcounty agencysamptype POPESTIMATE2010
by fstate fcounty, sort: gen samepop = cond(POPESTIMATE2010[1] == POPESTIMATE2010[_N], 1, 0)

Based on: https://www.stata.com/support/faqs/d...ions-in-group/

Thank you for your time!

Tom