Thanks that does work for these cases. I have found a case where it doesn't work and I think perhaps because there is no reference to the amt_reached and amt_needed columns. In reference to your previous question for the last 4 observations, the reason why I want it to be 1,0,1,2 is because the 3rd from last observation is as follows: ID Level amt_reached amt_needed What I want 156 0 37.09 37.09 0 Since the columns amt_reached and amt_needed fall are equal, then the new column will be at 0. If amt_reached < amt_needed, then it should be 1,2,3,0 for the last 4 observations. Whenever Level = 0 and amt_reached >= amt_needed then that row should = 0 and the count restarts at the next observation. However if Level = 1 and amt_reached >= amt_needed, then the new column should equal 1 at that point. These two columns are the main decision points. Hope that is a bit clearer. Here is an example of when it doesn't work with your new code: ID _Level new Level amt_reached amt_needed What I want 111 0 1 1 125.27 247.92 1 111 1 2 1 122.65 242.71 2 111 1 3 1 120.06 237.33 3 111 1 0 1 117.27 232.1 0 111 1 0 1 114.83 227.21 0 111 1 0 1 112.38 222.43 0 111 1 0 1 110.05 217.77 0 111 1 0 0 213.27 213.27 0 111 0 1 0 97.51 97.51 0 111 0 1 0 95.02 95.02 0 111 0 1 0 92.67 92.67 0 111 0 1 0 90.3 90.3 0 Thanks again!
... View more