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
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.