Greetings all,
dataset:
patient value
1 99
1 100
2 300
2 200
2 100
3 500
3 200
would like to get the following, using proc sql:
patient value
1 100
2 300
3 500
Normally if I wanted to do this, I would use proc sort by patient then descending value, then use a "if first.patient" then output, in order to get the max value. I'm thinking there may be an easier way to do this in one swoop using proc SQL.
Any help is appreciated, thank you.
Please try the below code
proc sql;
create table want as select a.* from have as a left join have as b on (a.patient=b.patient and a.value le b.value) group by a.patient,a.value
having count(*) <=1 order by a.patient, a.value desc;
quit;
Output:

Thanks,
Jag
Please try the below code
proc sql;
create table want as select a.* from have as a left join have as b on (a.patient=b.patient and a.value le b.value) group by a.patient,a.value
having count(*) <=1 order by a.patient, a.value desc;
quit;
Output:

Thanks,
Jag
Another option:
proc sql;
create table want as
select patient, max(value) from have
group by patient;
quit;
Tom
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.