Hello everyone,

I am dividing my panel data into 4 quartiles by country's GDP and year. A country may or may not be in the same quartile each year. But, for most part they will belong to same quartile. This dataset has 130 countries and 33 periods starting from 1984 to 2016.
For this, I run the following code:
Code:
 sort cty year

. forval x = 25(25) 75 {
  2. by year : egen p`x' = pctile(rgdpna), p(`x')
  3. }
rgdpna is the real GDP of a country. cty = country

However, I get an error message.
not sorted
r(5);
.

I tried including -sort- command in the -for- loop but didn't work either.

Could you tell me where am I going wrong?