Hello,

I want to generate a variable (max_count) wherein, for a given group ID, if the value of "count" for the current year is higher than the previous year then "max_count" takes the value of the current year. The value of the current year will be applied to the succeeding years until a higher value than the current year occurs. For instance, in the example below for ID 2, the value of "count" in 2001 is 10 but the succeeding years (2002 and 2003) have values less than 10 (i.e. 2 and 4) so 2002 and 2003 then take the value of 10 (the highest value after 2001).

I used this Stata code but it doesn't work: bysort id (Year): gen max_count=max(count, count[_n-1]) The highest value is only applied to the immediately succeeding year and not to all succeeding years.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte ID int Year byte(count max_count)
1 2000  0  0
1 2001  5  5
1 2002  7  7
1 2003  1  7
2 2000  5  5
2 2001 10 10
2 2002  2 10
2 2003  4 10
3 2000  2  2
3 2001  5  5
3 2002  9  9
3 2003  6  9
end
I hope I used dataex correctly. Please let me know if not.

Thanks a lot for your help! This question is also posted here: https://stackoverflow.com/questions/...up-using-stata