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

Hello, 

 

I have a dataset of hospital admissions with multiple observations per patient. I'm looking to delete patient records based on the value of a binary variable (RANK) in the dataset. The first record for each patient should have a value 1 for RANK. If there are records within the patient ID that have a 0 before a 1 for RANK, I want to delete them. Any records within each patient id with a 0 after 1 for RANK should be kept. Here is an example of the data:

 

data have:

RANK     ID

1             1

1             2

0             2

1             3

0             4

1             4

0             4

1             5

0             6

1             6

1             7

0            8

0            8

1            8

0            8

 

data want:

RANK     ID

1            1

1            2

0            2

1            3

1            4

0            4

1            5

1            6

1            7

1            8

0            8

 

 

I'm not sure how to go about this -- any coding suggestions are appreciated. 

Thank you.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Sort by ID and date (or whatever you got for a time series).

Do data step with

date want;
set have;
by id;
retain flag;
if first.id then flag = 0;
if rank = 1 then flag = 1;
if flag;
drop flag;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Sort by ID and date (or whatever you got for a time series).

Do data step with

date want;
set have;
by id;
retain flag;
if first.id then flag = 0;
if rank = 1 then flag = 1;
if flag;
drop flag;
run;
eabc0351
Quartz | Level 8

yes that does it, thank you.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 2 replies
  • 1120 views
  • 0 likes
  • 2 in conversation