Search code examples
stataeconomics

Keep individuals in the same firm by year (Stata)


I have an employer-employee database and need to keep only the individuals that have at least one colleague considering the Firm_id variable, but I don't know how to do this in Stata. My dataset is like this:

Id    Firm_id    Year
1        50       2010
1        50       2011
2        50       2010
2        50       2011
3        22       2010
3        22       2011
4        22       2010
4        20       2011

In the case above, I would keep only the individuals corresponding to the Id 1 and 2 because they are in the same firm in both of the years in the sample and Id 3 and 4 for 2010.

The output I'm looking for is like:

Id    Firm_id    Year
1        50       2010
1        50       2011
2        50       2010
2        50       2011
3        22       2010
4        22       2010

Any suggestions on how to perform this in Stata?

Regards,


Solution

  • bysort Id (Firm_id) : keep if Firm_id[1] == Firm_id[_N] 
    

    See FAQ here.