Hey there,

for a robustness test in my paper I need only those companies that are in the sample the wohle observation period from 2002 until 2019. I need to keep all companies that were listed in a certain stock market index form 2002 - 2019. I f certain company entered and dropped out of the index again it should be removed. Same if it entered later than 2002 and stayed till the end or if it dropped out before 2019.

Is there a way to do this?

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float company_code int year double(risk2 governance_pillar_score) byte sic_2 float operating_cash_flow_ratio
198 2002  1.391313791275024                 . 10   -.1932612
198 2003  .7691019177436829                 . 10    2.556747
198 2004  1.565499782562256                 . 10   .50846094
198 2005  1.561079502105713                 . 10    .3062136
198 2006  1.385605096817017                 . 10   .23397173
198 2007  1.730829000473022             .8184 10    .1633319
198 2008  2.183369398117065             .8236 10     .221042
198 2009  2.793251991271973 .8440000000000001 10   .07996524
198 2010  1.878877997398376 .8956000000000001 10  .024312966
198 2011  1.360489964485168             .9163 10  .002644395
198 2012  1.362555980682373             .9261 10 .0031669275
198 2013   1.62657618522644             .8314 10   .02201663
198 2014  .9175859093666077 .6052000000000001 10   .10074478
198 2015  1.936905860900879             .8207 10   .14188714
198 2016  1.013878226280212 .7687999999999999 10    .1574926
198 2017  1.975228190422058             .6722 10   .10259362
198 2018  1.311715722084045 .6423000000000001 10   .09886453
198 2019  1.858193397521973             .6672 10           .
359 2002  1.128613114356995             .2486 10     .370342
359 2003  .7857871651649475             .2437 10   1.3294857
359 2004  1.662140369415283             .3961 10    .1007927
359 2005  1.229512453079224             .4241 10    .7922229
359 2006   1.99222731590271 .7162000000000001 10    .8686776
359 2007  1.748663187026978             .3956 10    .0828804
359 2008  1.545645713806152             .6148 10   .07992224
359 2009  1.863270044326782 .8148000000000001 10   .14588326
359 2010  1.583709716796875 .7445999999999999 10    .1314661
359 2011  1.177901148796082 .8078000000000001 10    .1162769
359 2012  1.321306824684143             .7608 10   .15988074
359 2013  1.259462714195251 .6859999999999999 10   .05073183
359 2014  1.163728475570679 .8793000000000001 10   .11259461
359 2015  2.479107141494751 .9365000000000001 10   .14213024
359 2016  2.204270362854004             .9411 10   .17739904
359 2017  1.528578758239746             .9342 10   .17747037
359 2018  1.546251058578491 .9279999999999999 10    .1568126
359 2019  1.807860255241394 .9181999999999999 10           .
113 2002  .0710405632853508                 . 10   .07281148
658 2002  .0024976728018373                 . 10   .07814378
591 2002  .0372152402997017                 . 10   .16573374
113 2003  .0678781569004059                 . 10    .3958415
591 2003 -.0695282742381096                 . 10   .06509256
658 2003  .0371696799993515                 0 10           .
658 2004  1.158032059669495                 0 10           .
591 2004  .9739450812339783                 . 10   .24000365
113 2004  .8926684856414795                 . 10    .7069601
113 2005  .4928842484951019             .4827 10    .2934299
658 2005  .6290263533592224                 0 10           .
591 2005  .6111928820610046 .8470000000000001 10   .09588126
591 2006  1.155129551887512             .5598 10    .1936225
113 2006  .8849056959152222             .3378 10   .14376847
658 2006                  .                 0 10           .
658 2007                  .                 0 10           .
591 2007  .9670639038085938 .7073999999999999 10   .10589527
113 2007  1.040050268173218             .5807 10  -.12479202
658 2008                  .                 0 10           .
113 2008  .6590862274169922             .4502 10   .02846708
591 2008    .93929523229599             .8733 10   .14597794
591 2009  .4350540637969971             .9673 10    .1482539
113 2009  .4624072313308716 .7648999999999999 10   .15959626
658 2009                  .                 0 10           .
658 2010                  .                 0 10           .
113 2010  .5831794738769531             .6157 10    .1098726
591 2010  .6471161842346191 .8993000000000001 10   .07541484
658 2011                  .                 0 10           .
113 2011  .4087144732475281             .7603 10     .047422
591 2011  .7099542021751404             .9619 10   .02275898
591 2012  .2493201196193695             .9536 10   .04199664
658 2012                  .                 0 10           .
113 2012  .1048293709754944             .4891 10   .05162479
658 2013                  .                 0 10           .
113 2013  .6318023204803467             .4978 10   .02605677
591 2013  .5437512397766113 .9067000000000001 10   .05367707
113 2014  .1947876214981079             .6468 10     .140788
658 2014                  .                 0 10           .
591 2014  .3543002903461456 .9662999999999999 10    .0621811
591 2015  .7858543395996094             .9484 10   .06128537
113 2015  .8046262264251709             .6884 10    .1553322
658 2015                  .                 0 10           .
591 2016  .3263143301010132             .9462 10  .072994925
658 2016                  .                 0 10           .
113 2016 -.1227962896227837             .5183 10      .16617
658 2017                  .                 0 10           .
591 2017   .075228214263916              .988 10  .071806654
113 2017  .0821754634380341             .5962 10   .17814603
591 2018  .3042951226234436 .9859000000000001 10   .07078862
658 2018                  .                 0 10           .
113 2018  .1183760762214661             .6861 10   .17801325
113 2019 -.3050179779529572 .7937000000000001 10           .
591 2019 -.0263300165534019 .9684999999999999 10           .
658 2019                  .                 0 10           .
439 2002  1.100985646247864                 . 10           .
439 2003  .4134242832660675                 0 10           .
439 2004  1.362350702285767                 0 10  .069348596
439 2005  .8549619913101196                 0 10   .06724878
439 2006  .8449952006340027                 0 10           .
439 2007                  .                 0 10           .
439 2008                  .                 0 10           .
439 2009                  .                 0 10           .
439 2010                  .                 0 10           .
439 2011                  .                 0 10           .
end