BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hjjijkkl
Pyrite | Level 9

From the data below and I am tryign to eliminate those that have two consecutive test above 80. How Do I go about doing My analysis?  for example, in the table below, ID R45 will be excluded because it as two test above 80 consecutively  (test 90 and test 100). Thanks

ID Date test
R45 3/15/19 90
R45 4/15/19 100
R45 5/15/19 70
R45 6/15/19 60
R90 3/15/19 12
R90 4/15/19 120
R90 5/15/19 45
R90 6/15/19 100
R30 3/15/19 72
R30 4/15/19 28
R30 5/15/19 50
R30 6/15/19 220

 

my output would look like this

ID Date test
R90 3/15/19 12
R90 4/15/19 120
R90 5/15/19 45
R90 6/15/19 100
R30 3/15/19 72
R30 4/15/19 28
R30 5/15/19 50
R30 6/15/19 220
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @hjjijkkl,

 

If your dataset is grouped by ID and sorted by Date within each ID BY group, you can use a so-called double DOW-loop to eliminate the IDs in question:

data have;
input ID $ Date :mmddyy. test;
format date mmddyy8.;
cards;
R45 3/15/19 90
R45 4/15/19 100
R45 5/15/19 70
R45 6/15/19 60
R90 3/15/19 12
R90 4/15/19 120
R90 5/15/19 45
R90 6/15/19 100
R30 3/15/19 72
R30 4/15/19 28
R30 5/15/19 50
R30 6/15/19 220
;

data want(drop=tbd);
do until(last.id);
  set have;
  by id notsorted;
  if ~first.id & test>80 & lag(test)>80 then tbd=1; /* "to be deleted" */
end;
do until(last.id);
  set have;
  by id notsorted;
  if ~tbd then output;
end;
run;

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hello @hjjijkkl,

 

If your dataset is grouped by ID and sorted by Date within each ID BY group, you can use a so-called double DOW-loop to eliminate the IDs in question:

data have;
input ID $ Date :mmddyy. test;
format date mmddyy8.;
cards;
R45 3/15/19 90
R45 4/15/19 100
R45 5/15/19 70
R45 6/15/19 60
R90 3/15/19 12
R90 4/15/19 120
R90 5/15/19 45
R90 6/15/19 100
R30 3/15/19 72
R30 4/15/19 28
R30 5/15/19 50
R30 6/15/19 220
;

data want(drop=tbd);
do until(last.id);
  set have;
  by id notsorted;
  if ~first.id & test>80 & lag(test)>80 then tbd=1; /* "to be deleted" */
end;
do until(last.id);
  set have;
  by id notsorted;
  if ~tbd then output;
end;
run;
Ksharp
Super User
data have;
input ID $ Date :mmddyy. test;
format date mmddyy8.;
cards;
R45 3/15/19 90
R45 4/15/19 100
R45 5/15/19 70
R45 6/15/19 60
R90 3/15/19 12
R90 4/15/19 120
R90 5/15/19 45
R90 6/15/19 100
R30 3/15/19 72
R30 4/15/19 28
R30 5/15/19 50
R30 6/15/19 220
;
data temp;
 set have;
 flag=ifn(test>80,1,0);
run;
proc summary data=temp;
by id flag notsorted;
output out=temp1;
run;
proc sql;
create table want as
select * from have where id not in (select id from temp1 where flag=1 and _freq_>1);
quit;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 428 views
  • 2 likes
  • 3 in conversation