The Following code should run industry and year regressions excluding company i and then estimate the slope coefficients. The existing code has only one sample restriction, which is that the number of firms per each industry and year group is greater than 25:

Code:
 
 local X inv exp uti acc rd  cf
tokenize "`X'"

gen adjr2=.
forval j = 0/6 {
    gen b`j'=.
}

forval i = 1/`=_N' {
    local same sic_2[`i'] == sic_2 & yr[`i'] == yr
    qui count if `same' & _n != `i'

    qui if r(N) > 25  {
         reg pay `X' if `same' & _n != `i'

         if _rc == 0 {
              replace b0 = _b[_cons] in `i'
              replace adjr2=e(r2_a) in `i'
              forval j = 1/6 {
                    replace b`j' = _b[``j''] in `i'
              }
         }
    }
}
My question is that how can I edit the code to add the following additional restrictions:
For each industry and year group, the number of firms that reports a non-zero dividend variable (use div for dividend) is greater than a quarter of the number of firms in each industry year group and must be greater than 10 firms per industry and year group ? Note that div is not a variable in the regression.

My aim is to ensure that the regression above is estimated when there is a sufficient number of firms in each industry and year group that reports non-zero div. Note that sic_2 is my industry identifier.

Thank in advance