Any easy solution will need to make two passes through the data. If two passes through the data is not practical, we can always revisit. So assuming that your data is already in sorted order, and assuming that you followed the suggested solution to create FLAG, process again:
data want;
set want;
by subject test flag notsorted;
if flag=1 and (first.flag=0 or last,.flag=0) then elevated_consecutive_flag='*';
run;
Optionally, you could drop FLAG at that point as well.
Hii
i have a dataset like this
subject | test | visit | high_range | actual_value |
101 | A | 1 | 30 | 100 |
101 | A | 2 | 30 | 90 |
101 | A | 3 | 30 | 80 |
101 | A | 4 | 30 | 60 |
101 | A | 5 | 30 | 55 |
101 | A | 6 | 30 | 65 |
101 | A | 7 | 30 | 70 |
101 | A | 8 | 30 | 95 |
101 | B | 1 | 40 | 130 |
101 | B | 2 | 40 | 150 |
101 | B | 3 | 40 | 65 |
101 | B | 4 | 40 | 85 |
101 | B | 5 | 40 | 120 |
101 | B | 6 | 40 | 135 |
and i want to flag the >= 3 time elevated values into one variable
then i want to flag the consecutive flags into another variable like this and that to by subject and test
output like this
subject | test | visit | high_range | actual_value | flag_>=3t_elev | consec_flags |
101 | A | 1 | 30 | 100 | y | y |
101 | A | 2 | 30 | 90 | y | y |
101 | A | 3 | 30 | 80 | n | n |
101 | A | 4 | 30 | 60 | n | n |
101 | A | 5 | 30 | 55 | n | n |
101 | A | 6 | 30 | 65 | n | n |
101 | A | 7 | 30 | 70 | n | n |
101 | A | 8 | 30 | 95 | y | n |
101 | B | 1 | 40 | 130 | y | y |
101 | B | 2 | 40 | 150 | y | y |
101 | B | 3 | 40 | 65 | n | n |
101 | B | 4 | 40 | 85 | n | n |
101 | B | 5 | 40 | 120 | y | y |
101 | B | 6 | 40 | 135 | y | y |
can you please help me out.
Thanks
OK, putting together all the previous suggestions, and taking the simplest route:
proc sort data=have;
by subject test visit;
run;
data want;
set have;
if actual_value >= 3*high_range then flag_3xElev='y';
else flag_3xElev='n';
run;
data want;
set want;
by subject test flag_3xElev notsorted;
if flag3xElev='y' and (first.flag_3xElev=0 or last.flag_3xElev=0) then consec_flags='y';
else consec_flags='n';
run;
Good luck.
Art,
I don't know ... I didn't do anything to make it happen. Funny thing is, it now appears in my Content section even though I didn't really initiate it.
All questions and no answers on that one.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.