Greetings statalist users,

I need help with codes to eliminate the entire id (along with all its observations across all vars) for banks (id) that does not have 3-consecutive years or more of observations. In other words i want to keep banks in my sample that has 3-consecutive years minimum observations.

Now the tricky part for me that i need the code that allows to drop those banks based on specific variable (consecutive years) and a code that just drop banks with no 3-consecutive across all variables. since the latter might significantly reduce my sample size.


My sample:
Code:
xtdescribe
"id refers to individual banks"
      id:  11393, 12770, ..., 99921                          n =       1096
    year:  2002, 2003, ..., 2019                             T =         18
           Delta(year) = 1 unit
           Span(year)  = 18 periods
           (id*year uniquely identifies each observation)
My data look like:
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long id int year double(EA AssetG) float ZS_3
11393 2002     .      .         .
11393 2003     .      .         .
11393 2004     .      .         .
11393 2005     .      .         .
11393 2006     .      .         .
11393 2007     .      .         .
11393 2008     .      .         .
11393 2009     .      .         .
11393 2010     .      .         .
11393 2011     .      .         .
11393 2012 14.54      .         .
11393 2013 12.07  -1.89  6.098352
11393 2014  10.8   3.39  6.054359
11393 2015 10.41    1.5  5.482283
11393 2016 11.48  54.93  4.765625
11393 2017 11.87    .34   4.38668
11393 2018     .      .         .
11393 2019     .      .         .
12770 2002     .      .         .
12770 2003     .      .         .
12770 2004     .      .         .
12770 2005     .      .         .
12770 2006     .      .         .
12770 2007     .      .         .
12770 2008     .      .         .
12770 2009     .      .         .
12770 2010     .      .         .
12770 2011 39.92      .         .
12770 2012 35.45   6.68         .
12770 2013 32.23  12.42  2.918765
12770 2014 31.75   30.6 2.0367548
12770 2015  54.5 -25.49  2.671301
12770 2016 57.06  12.61 2.4895585
12770 2017 36.06   63.3  2.736582
12770 2018 32.25   1.52  2.818459
12770 2019 17.52  54.26 2.2408023
13171 2002     .      .         .
13171 2003     .      .         .
13171 2004     .      .         .
13171 2005  5.67      .         .
end
and i tried this code that i found and did not delete any observation
Code:
egen tag = tag(id year)
egen ntag = total(tag), by(id)
drop if ntag < 3
results
Code:
(0 observations deleted)
also tried this code
Code:
 rangestat (count) year, interval(year -2 0) by(id)
. bysort id (year_count): gen to_drop4 = year_count[_N] < 3
. sort id year
. assert to_drop1 == to_drop2
results
Code:
 to_drop1 not found
r(111);
I also looked at many other posts and links that either i did not understand or they did not work.

Finally, this is only my second post and i hope i followed the instruction for statalist forum guidelines. if i missed something please do tell me.
I appreciate your help.
Kind regards,
Sad