I have a cross-section of data on school applications. Each student applies at multiple schools and needs a score higher than the school minimum to get in. I would like to know for each school the total number of students that have a score above it's minimum, irrespective of whether the student applied to this school or not. The final dataset is very large and I tried some foreach approaches but they did not work.


Code:
clear
input id score school_code school_minimum
1 50 5678 54
1 50 3113 60
1 50 1001 45
2 55 4143 52
2 55 5678 54
2 55 3113 60
3 51 3113 60
3 51 3098 50
3 51 1001 45
4 62 3098 50
4 62 3113 60
4 62 8877 62
end

Code:
levelsof school_minimum, local(cutoffs) 
levelsof id, local(students) 

gen num_higher_score = .

foreach c in `cutoffs' {
local count_`c' = 0

foreach id in `students' {

if score > `c' local ++count_`c'

bysort school_code: replace num_higher_score = `count_`c'' if cutoff == `c'

}
}