Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

stata - Filter specific observations

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:

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 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. Individual number 3 in 2011 and Individual 4 in 2011 would be dropped.

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
question from:https://stackoverflow.com/questions/65557857/filter-specific-observations

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This works for your data example:

clear 
input 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
end

bysort Year Firm_id : keep if Id[1] != Id[_N]
sort Id Year 
list 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...