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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag

View solution in original post

2 REPLIES 2
Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag
TomKari
Onyx | Level 15

Another option:

proc sql;

create table want as

select patient, max(value) from have

group by patient;

quit;

Tom

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 connect to databases in SAS Viya

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.

Discussion stats
  • 2 replies
  • 1293 views
  • 3 likes
  • 3 in conversation