Hi Statlist users,

I am currently having an issue counting the unique number of observation conditional on another variable.

In particular, I want to do the following:
Session_district Facility_id Count
1001 A 2
1001 B 3
1001 A 2
1002 B 3
1002 A 2
1003 B 3
1003 B 3
1001 C 1
1001 C 1
1001 C 1
For each facility_id, I want to count the number of observation based on the unique value of session_district.
Hence, as illustrated in the example, although facility_id will occur distinctly 3 times, because there are only two unique session_district, the count would be 2.


I tried code:
egen count = nvals(session_district), by(facility_id) would give me count for distinct observations by session_district, i.e. facility_id A would get count equal to 3, instead of 2, which is what I want.

Could someone provide some help with this? I could not really find the answer to this elsewhere.
Thanks!