here is an example,
Q: keep the first 3 states, which have highest values of "public" in each region across years
clear
webuse productivity
gsort year region -public
bys year region : gen rank=_n
//mark those which will be deleted (delete=1)
gen delete= rank>3
l year region state public rank delete in 1/100