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

Hello

I want to use proc sql to select one row by group.

The criteria is to select  for each group the row with max value of date.

However, if there are multiple rows with max date value then this query is not working well because it select multiple rows by group.

What is the way to solve it via proc sql?

 

data have;
input ID $ date : date9. var;
format date date9.;
cards;
B 05JAN2023 5
D 24JAN2023 2
B 06JAN2023 6
B 06JAN2023 6
B 06JAN2023 6
B 06JAN2023 6
C 15JAN2023 7
A 01JAN2023 10
C 16JAN2023 8
C 19JAN2023 9
C 20JAN2023 10
C 20JAN2023 10
C 20JAN2023 10
;
Run;

proc sql;
create table Way1 as
SELECT *
from have
group by ID
having date=max(date)
;
quit;

 

 

 

 

1 ACCEPTED SOLUTION
2 REPLIES 2
Ronein
Meteorite | Level 14

Great

proc sql;
create table want as
SELECT distinct *
from have
group by ID
having date=max(date)
;
quit;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 76 views
  • 1 like
  • 2 in conversation