data have;
infile cards dlm='09'x;
input ID value seq_id;
cards;
1 3 1
1 1 2
1 2 3
1 4 4
1 1 5
2 20 1
2 21 2
2 5 3
3 1 1
3 6 2
3 7 3
3 1 4
3 4 5
3 21 6
3 17 7
3 1 8
3 2 9
4 6 1
4 9 2
4 12 3
4 13 4
5 1 1
5 6 2
5 7 3
5 14 4
5 20 5
6 11 1
6 12 2
6 25 3
6 16 4
6 21 5
;
run;
proc format library=work;
value labelfmt
1 = 'too high'
2 = 'high'
3 = 'low'
4 = 'normal'
;
run;
data want (keep=id output);
set have;
by id;
retain flag;
if first.id then flag = ' ';
lagval = lag(value);
if value > 20 then substr(flag,1,1) = 'x';
else if not first.id
then do;
if 16 <= value <= 20 and 16 <= lagval <= 20 then substr(flag,2,1) = 'x';
else if 10 <= value <= 20 and 10 <= lagval <= 20 then substr(flag,3,1) = 'x';
else substr(flag,4,1) = 'x';
end;
if last.id
then do;
pos = index(flag,'x');
output = put(pos,labelfmt.);
output;
end;
run;
Note that in your example, group 4 was incorrectly labeled "high", as values 12 & 13 fall into your "low" category.
PS although I used your variable name "output", I recommend to not use SAS keywords as variable names.
... View more