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.
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;
What is it that you are trying to do?
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.