Dears

I would like to generate a new variables MS & PS as it show in the below:


Array


I used audit fees as X.


According to the previous studies, I generate a dummy variable to measure if an auditor is a specialist and coded as 1 if MS greater than 30% and 0 otherwise. I used the following codes and kindly advise me the codes incorrect:
Code:
bysort sic2 year : egen audit1= sum(audit_fees)
bysort sic2 year audit_ID: egen audit2= sum(audit_fees)
gen MS = audit2 /audit1
gen MS_DUMMY = 0 
replace MS_DUMMY = 1 if MS>.30
I have the following questions:

1- How can I measure a dummy variable if an auditor is a specialist "when an auditor has a market share that is the highest in a given industry and also more than 10 percent higher than the next-largest competitor during the year, and is 0 otherwise".

2- How can I measure PS that shows in the equation above?

3- Some studies use "the ratio of audit fees that an audit office generates in a two-digit SIC industry to the total audit fees generated by an audit office in a Metropolitan Statistical Area (MSA) for a given year". I am stuck at this point and have some difficulties to measure it.






ID : Firms ID.
audit_ID: Auditor ID or key.
sic2: Two-digit of Standard Industry Classification Code (Industry Code).
msa_code: Metropolitan statistical area code (City code).


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long ID double year int audit_ID double audit_fees float sic2 double msa_code
1608 2005     4 1265000 37 16980
1608 2006     4 1335000 37 16980
1608 2007     4 1325000 37 16980
1608 2008     4 1440000 37 16980
1608 2009     4 1490000 37 16980
1608 2010     4 1275000 37 16980
1608 2011     4 1745640 37 16980
1608 2012     4 1689980 37 16980
1608 2013     4 1794370 37 16980
1711 2005   748   53000 37 31100
1711 2006   748   54000 37 31100
1711 2007   748   56000 37 31100
1711 2008  5644   41500 37 31100
1711 2009 11556   10750 37 31100
1781 2005     3  173000 15 12060
1781 2006     3  175000 15 12060
1781 2007     3  185000 15 12060
1781 2008     3  203500 15 12060
1781 2009     3  210000 15 12060
1781 2010     3  220000 15 12060
1819 2005   598   12200 73 14460
1819 2006   598   12100 73 14460
1819 2007   598   10000 73 14460
1819 2008   598   17000 73 14460
1819 2009   598   23000 73 14460
1819 2010   598   26500 73 14460
1819 2011   598   26500 73 14460
1819 2012   598   26500 73 14460
1819 2013   598   26500 73 14460
1892 2005     4 1294000 51 35620
1892 2006     7  854000 51 35620
1892 2007     7  912000 51 35620
1892 2008     7  982000 51 35620
1892 2009     7  962000 51 35620
1892 2010 11761  886000 51 35620
1892 2011 11761  959000 51 35620
1892 2012 11761  943000 51 35620
1892 2013 11761  981000 51 35620
1956 2005     2  183000 34 14860
1956 2006     2  220000 34 14860
1956 2007     2  231000 34 14860
1956 2008     2   43000 34 14860
1956 2009  1687  192000 34 14860
1956 2010  8256  173000 34 14860
1956 2011  8256  174000 34 14860
1956 2012  8256  180000 34 14860
1956 2013  8256  188000 34 14860
1993 2005     1 2773000 73 19100
1993 2006     1 3741000 73 19100
1993 2007     1 5142000 73 19100
1993 2008     1 4484000 73 19100
1993 2009     1 3029000 73 19100
2036 2005     3  338100 51 26420
2036 2006     3  425534 51 26420
2036 2007     3  544747 51 26420
2036 2008     3  634253 51 26420
2036 2009     3  846528 51 26420
2036 2010     3  741268 51 26420
2036 2011     3  610922 51 26420
2036 2012     3  827934 51 26420
2036 2013     3  889800 51 26420
2044 2005     7  150500 36 37340
2044 2006     7  141000 36 37340
2044 2007     7  181500 36 37340
2044 2008     7  170000 36 37340
2044 2009     7  183750 36 37340
2044 2010 11761  125000 36 37340
2044 2011 11761  181700 36 37340
2044 2012 11761  181700 36 37340
2044 2013 11761  181700 36 37340
2346 2005     2 9300000 36 41940
2346 2006     2 4500000 36 41940
2346 2007     2 5100000 36 41940
2346 2008     2 4400000 36 41940
2346 2009     2 4000000 36 41940
2346 2010     2 3610790 36 41940
2346 2011     2 3325280 36 41940
2346 2012     2 3485230 36 41940
2346 2013     2 3868890 36 41940
2349 2005     3 3345470 73 29820
2349 2006     3 6502200 73 29820
2349 2007     3 4864620 73 29820
2349 2008     3 5127300 73 29820
2349 2009     3 2824200 73 29820
2349 2010     3 2029550 73 29820
2349 2011     3 1804370 73 29820
2349 2012     3 1900470 73 29820
2349 2013     3 2050300 73 29820
2459 2005     4 3977000 36 35620
2459 2006     4 2872000 36 35620
2459 2008     4 2300000 36 35620
2459 2009     4 1900000 36 35620
2459 2010     4 2200000 36 35620
2827 2005     4 6000000 28 10900
2827 2006     4 5200000 28 10900
2827 2007     4 5500000 28 10900
2827 2008     4 5700000 28 10900
2827 2009     4 6000000 28 10900
2827 2010     4 5700000 28 10900
2827 2011     4 6100000 28 10900
end