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

Hello, 

 

I would like to delete the ID with more than 2 coutinous 0 value of RX. I am wondering what is efficient way to do it.   Thank you very much.

 

ID    RX     

1        1

1       1

1       0

1        1

1       0

1        1

2        0

2       0

2        1

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So want to look for two in a row?  You didn't mention any variable to use for ordering so we should we assume the data is already sorted?  

So two is easy. Basically if RX=0 and it is not an isolated block of just one RX=0 then you have two or more RX=0 records in a row.

Using double DOW loop you can test an ID and then in the second loop decide whether to output the records for that ID.

data want ;
 do until (last.id);
   set have ;
   by id rx notsorted ;
   if rx=0 and not (first.rx and last.rx) then any2=1;
 end;
 do until (last.id);
   set have ;
   by id rx notsorted ;
   if not any2 then output;
 end;
run;

It looks like both of your example IDs would be deleted since the both have two or more RX=0 records next to each other.

 

View solution in original post

3 REPLIES 3
thomp7050
Pyrite | Level 9

I prefer proc sql because the code/concept may be used in many applications.

 

For your consideration:

 

PROC SQL;
CREATE TABLE TOTALS AS
SELECT ID, SUM(RX) AS TOTAL FROM IDS GROUP BY ID;
QUIT;

PROC SQL;
CREATE TABLE NEWTABLE AS
SELECT * FROM IDS WHERE ID IN (SELECT ID FROM TOTALS WHERE TOTAL <=2);
QUIT;
Ivy
Quartz | Level 8 Ivy
Quartz | Level 8
Thank you, we need to consider the >= 2 continuous 0 instead of total 0 . For example, there are two 0 for ID 1, but it cannot be deleted, due to not 0 is separated.
Tom
Super User Tom
Super User

So want to look for two in a row?  You didn't mention any variable to use for ordering so we should we assume the data is already sorted?  

So two is easy. Basically if RX=0 and it is not an isolated block of just one RX=0 then you have two or more RX=0 records in a row.

Using double DOW loop you can test an ID and then in the second loop decide whether to output the records for that ID.

data want ;
 do until (last.id);
   set have ;
   by id rx notsorted ;
   if rx=0 and not (first.rx and last.rx) then any2=1;
 end;
 do until (last.id);
   set have ;
   by id rx notsorted ;
   if not any2 then output;
 end;
run;

It looks like both of your example IDs would be deleted since the both have two or more RX=0 records next to each other.

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1029 views
  • 0 likes
  • 3 in conversation