I have a dataset that looks as follows. For each state, I know which individuals are accepted (acc = 1) or rejected (acc = 0) on the basis of two scores x and y. A want to say that a rejection is bad if there is an individual in that state that is accepted and that has worse scores on both x and y. I can compute this looping through states, accepted, and rejected candidates, see below. However, I was looking for suggestions on how to make the program more efficient.

gen obs = _n


gen bad = 0 if acc = 0
gen obs = _n
glevelsof state if acc == 1
local stloop "`r(levels)'"

foreach s in `stloop' {

glevels of obs if state == `s' & acc == 1
local accit "`r(levels)'"
glevelsof obs if fstate == `s' if acc == 0
local nsuit "`r(levels)'"

foreach i in `nsuit' {
foreach ac in `accit' {
if x[`i'] > x[`ac'] & y[`i'] > y[`ac'] {
replace bad = 1 if obs == `i'
continue ,break
}
}
}

}