Hello all,

I have panel data for years 1998-2020 about firm performance (stock returns). For my future regression model I need to use the data to make several peer groups namely size, industry, size-industry. For size peer groups I have split the data into 4 quartiles already. For the size-industry peer groups I need to first separate by industry (SIC) and then within that industry classification sort the relevant firms into firm size quartiles. I would like to create a new variable with the mean Return of the peers within each size-industry peer group (excluding the Ret of the company itself). For example, all firms that have an SIC code of 5080 and are in the third quartile in terms of Firm Size (within the selection of those with this SIC code) should be in one peer group. Each observation should have an additional variable created that shows the mean Ret of its peers (excluding itself). All of these peer groups should be constructed per year of course in order to be directly comparable. Please let me know if more information is needed to solve this. Thank you in advance!

Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input YEAR GVKEY FirmSizeQuartiles SIC Ret
2014 "001004" 3 5080   .04133066749753115
2015 "001004" 3 5080  .030511271300266735
2016 "001004" 3 5080   .30706653804452416
2017 "001004" 3 5080   .18616716883920895
2018 "001004" 3 5080  -.02262825649451842
2019 "001004" 3 5080    .2231935663768573
2013 "001045" 4 4512                    0
2014 "001045" 4 4512    .8415493288704637
2015 "001045" 4 4512  -.17650238089119838
2016 "001045" 4 4512   .17073748844470268
2017 "001045" 4 4512    .1353606589982849
2018 "001045" 4 4512  -.36499018856917903
2019 "001045" 4 4512  -.02171545035574331
2000 "001056" 1 3825    .7942321914001037
2001 "001056" 1 3825  .004744022674494005
2002 "001056" 1 3825   -.5122880228451151
2003 "001056" 1 3825    .6655166811595907
2004 "001056" 1 3825   .10205846156049947
2005 "001056" 1 3825  -.03859568835002513
2006 "001056" 2 3825   .10293606667514373
2015 "001072" 2 3670  -.09403120412056296
2016 "001072" 2 3670   .28737387954643445
2017 "001072" 3 3670   .13258199640124585
2018 "001072" 3 3670 -.024730791381781084
2007 "001075" 3 4911  -.10725982161935609
2008 "001075" 3 4911  -.18650988603877838
2009 "001075" 3 4911    .2268891870607131
2010 "001075" 3 4911   .17268959199841247
2011 "001075" 3 4911    .1857807090896215
2012 "001075" 3 4911   .09092921043026954
2013 "001075" 3 4911   .07965531555375924
2014 "001075" 3 4911    .2742602315448607
2015 "001075" 3 4911 -.004172655892773795
2016 "001075" 3 4911   .20234815144763957
2017 "001075" 3 4911   .10843727766739865
2018 "001075" 3 4911   .03622570923212827
2019 "001075" 3 4911   .07598462828538802
1998 "001078" 4 3845   .43580184152496615
1999 "001078" 4 3845    -.247286463341008
2000 "001078" 4 3845   .32119312636978986
2001 "001078" 4 3845   .17023644904397983
2002 "001078" 4 3845   -.2368203183565871
2003 "001078" 4 3845   .17500209624754212
2004 "001078" 4 3845   .08722009408565695
2005 "001078" 4 3845  -.10712031745255415
2006 "001078" 4 3845    .2032604995055375
2007 "001078" 4 3845   .13882128917840117
2008 "001078" 4 3845 -.011233185887486393
2009 "001078" 4 3845   .05870697174625365
2010 "001078" 4 3845 -.056174434307530545
2011 "001078" 4 3845   .15381778605683555
2012 "001078" 4 3845    .1423708650576156
2013 "001078" 4 3845    .1738669469608737
2014 "001078" 4 3845   .13811481171937817
2015 "001078" 4 3845  .029822520094305652
2016 "001078" 4 3845  -.06936963370529431
2017 "001078" 4 3845   .30209761505691085
2018 "001078" 4 3845    .1887153194903311
2019 "001078" 4 3845   .14203424444077295
2012 "001161" 4 3674   -.5715821955145768
2013 "001161" 4 3674    .5769143879090025
2014 "001161" 4 3674  -.30173831994521017
2015 "001161" 3 3674   .18457044546315918
2016 "001161" 4 3674   1.5235932261146117
2017 "001161" 4 3674  .008462946915528976
2018 "001161" 4 3674    .8420964422787784
2019 "001161" 4 3674    .8898099175752926
2004 "001164" 4 4813    .3303923166506873
1998 "001203" 3 4731  -.22053356067070676
2014 "001209" 4 2810   .29552307943428663
2015 "001209" 4 2810  -.06247420812233013
2016 "001209" 4 2810   .22885322514359116
2017 "001209" 4 2810   .15349147539065047
2018 "001209" 4 2810     .009272385601108
2019 "001209" 4 2810   .38966674018809094
2020 "001209" 4 2810   .18792437471192985
2000 "001213" 3 4513   -.6394474507645828
2001 "001213" 3 4513    .6387110075880933
2002 "001213" 3 4513    .1259535281062368
2007 "001230" 3 4512   -.3799135041517041
2008 "001230" 3 4512   .28391469192353586
2009 "001230" 3 4512   .26870307836836627
2010 "001230" 3 4512    .5186182946895369
2011 "001230" 4 4512     .280477948353573
2012 "001230" 4 4512   .14096014649099517
2013 "001230" 4 4512    .5374544393543125
2014 "001230" 4 4512    .4717349458620397
2015 "001230" 4 4512   .29185619746588504
2016 "001230" 4 4512   .14928963162911985
2017 "001230" 4 4512  -.11848322511693453
2018 "001230" 4 4512  -.09498970948059714
2019 "001230" 4 4512    .1241551081972381
2002 "001246" 4 5040   -.3647760906341976
2003 "001246" 4 5040    .6162295182087232
2004 "001246" 4 5040  .016911603231813285
2005 "001246" 4 5040 -.059521963893017595
2006 "001246" 3 5040    .4417522025271493
2007 "001246" 3 5040  -.16584304735033525
2012 "001254" 3 4400  -.08673422472408564
2013 "001254" 3 4400   .10186802603055975
end