Hello all,

I have a dataset that looks something like this (but with over 70k observations and over 200 firms)

FirmID Area dummy
1 10 1
1 10 1
1 11 1
1 11 1
1 12 0
2 10 1
2 11 0
3 10 1
3 11 1
3 11 1
3 12 1
3 12 1
3 13 0


Each row represents a customer of the firm in that area. And the dummy reflects whether this area is part of the firm's market or not. Now using this data, I want to calculate the total number of customers (including those from other firms) belonging to all areas that constitute a market for a firm, which I am able to do using -by sort- and -egen-. However, after that, or each firm, I want to identify the firms for the customers (and the corresponding number of customers from these firms) in that firm's market. I'm wondering what's a quick way to do this in Stata/

Thank you in advance for any ideas here. And I'm happy to answer any questions if my ask isn't clear!