Hi, I am still quite new to Stata (using Stata 16) and hope to find help here as I do not know how to solve this problem.

I have panel data (S&P 500) for roughly 20 years and a plethora of sic2 industries.

What I want to do is fo a certain variable (e.g., experience) for each sic2-industry and each year find the top performers (top decentile) and then create a variable where this "benchmark" can be recorded to. So in the end each firm (even if not a top performer) has the top performer average of this sic2 and year to compare to using this benchmark variable. I am using weighted average by sales.

So my benchmark variable is price-cost margin:

gen pg_margin = (sale-(cogs+xsga))/sale
egen pg_deciles = xtile(pg_margin), by(fyear sic2) nq(10)

// now I can claim that every firm firm with a "10" in pg_deciles is a top performer
// I further generate a dummy variable for industry leader to mark them

gen industrylead_sic2 = 0 // set up variable
replace industrylead_sic2 = 1 if pg_deciles==10

gen industrylead_experience = 0 // I create a new variable for experience in industry leaders (the benchmark variable)
replace industrylead_experience = experience if industrylead_sic2 == 1 // I copy over the experience of only the industry leaders in the new variable
bysort fyear sic2: egen wtavgindustrylead_experience = wtmean(industrylead_experience), weight(sale) // create an avg industryleader group per year per SIC2 weighted by sales

I have two questions:
1. Is this the correct way to do this or am I missing something?
2. How do I exclude the focal firm itself from the industry leader group if it is a top performer itself? And do I even need to if I want to avid endogeneity?

Thank you!