BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Newtrix
Fluorite | Level 6

I'm trying to subset a dataset into the greatest value of a single outcome within the multiple visits of each patient, so that each patient is listed once with his/her "best response":

 

PROC SORT DATA=Rank;

BY Patient Response;

RUN;

DATA Best_Response;

SET Rank;

BY Patient;

IF FIRST.Patient = 1;

RUN;

 

As written, this code changes the value of "Best_Response" for every patient to the best value possible in the code. How do I fix? Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
quickbluefish
Barite | Level 11
I think you just want:
IF LAST.patient;

...rather than FIRST.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

That code is NOT changing the values of any variable.

 

What it is doing is reducing the number of observations per PATIENT to only one.  The observation with the minimum value of RESPONSE for that patient.  So if PATIENT=1 had three observations with RESPONSE values of 2, 7 and 9 then the output dataset will only keep the observation where RESPONSE was 2 since it would be the first one for PATIENT=1 after the sort.

 

Example:

title;
data have;
  input patient response other;
cards;
1 2 10
1 7 4
1 9 50
2 1 1
3 9 99
3 8 88
3 7 77
;

proc sort;
  by patient response;
run;

proc print;
run;

data want;
  set have ;
  by patient;
  if first.patient;
run;

proc print;
run;

Tom_0-1734655068796.png

 

 

What is it that you are trying to do?

quickbluefish
Barite | Level 11
I think you just want:
IF LAST.patient;

...rather than FIRST.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 723 views
  • 2 likes
  • 3 in conversation