I want to mark a row if it has the greatest non-missing value among the last N rows. Below is an example where N=3: data have;
infile datalines delimiter = "," missover;
input value;
datalines;
.
.
1.5
.
.
.
.
1.1
.
2.0
.
.
.
2.2
1.7
.
.
.
run; data want;
infile datalines delimiter = "," missover;
input value mark count;
datalines;
., 0, .
., 0, .
1.5, 1, 1
., 0, 2
., 0, 3
., 0, .
., 0, .
1.1, 1, 1
., 0, 2
2.0, 1, 1
., 0, 2
., 0, 3
., 0, .
2.2, 1, 1
1.7, 0, 2
., 0, 3
, 0, .
., 0,.
run; I attempted to do this with retain command but didn't get what I want: data get; set have; mark = 0; retain value_old; if value ne . and value > value_old then do; mark = 1; count = 1; value_old = value; end; if lag(count) > 0 then count = lag(count) + 1; if (value = . or value < value_old) and lag(count) = 3 then do; count = .; value_old = .; end; run;
... View more