st: loops

I am dealing with huge datasets for which I have to estimate
percentile exposure time within two to three subsets. I noticed
something that happened there and tried duplicating it with a stata
example dataset [webuse beetle] and I noticed it again. Attached
below is the do file modified for beetle dataset.