Hello all,

I have a paneldataset of 18 years and 505 companies and 25 variables (one of those is firm_beta as a proxy for systematic risk).

I need to drop all the data for all the companies were there is no firm beta within the last 2 years.

I implemented this code and it indeed drops the companies where there is noc beta within the last two years but it drops some more companies and i do not know why.

Code:
sort company_key
keep if inrange(year, 2002, 2019)
bysort company_key: egen tot_miss_beta = count(firm_beta)
drop if tot_miss_beta < 16
I would appreciate some help.
Thanks, Patrick