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

Hi all!

 

I have a dataset like this

 

 

Group   A   B   C   X

    1       5    3   1   12

    1       6    4   1   9

    1       7    6   3   10

    2       4    2   8   7

    2       3    7   5   8

    2       5    2   4   13

    3       2    10  5   9

    3       4    9   3  15

    3       7    6   7   14

 

 

For each group, I need to extract only one row per group at a specific value of X (namely the max value of X in each group)

 

My final dataset should be like this

 

Group   A   B   C   X

    1       5    3   1   12

    2       5    2   4   13

    3       4    9   3  15

 

 

Tks in advance

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
tsap
Pyrite | Level 9
DATA WORK.HAVE;
FORMAT 	 Group $1. A 8. B 8. C 8. X 8.;
INFORMAT Group $1. A 8. B 8. C 8. X 8.;
INPUT    Group 	   A 	B 	 C 	  X;
INFILE DATALINES DLM='|' DSD;
DATALINES;
1|5|3|1|12
1|6|4|1|9
1|7|6|3|10
2|4|2|8|7
2|3|7|5|8
2|5|2|4|13
3|2|10|5|9
3|4|9|3|15
3|7|6|7|14
;

PROC SORT DATA=WORK.HAVE; BY GROUP X; RUN;

DATA WORK.WANT;
	SET WORK.HAVE;
	BY GROUP X;
	IF LAST.GROUP AND LAST.X;
RUN;

 

By sorting the data by 'Group' and 'X', in the following datastep we can utilize the 'last.variable' feature. So the last iteration within each unique group id with the highest 'X' value within that unique group id.

 

Output:

Group	A	B	C	X
1	5	3	1	12
2	5	2	4	13
3	4	9	3	15

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20
proc sql noprint;
create table want as
select *
from have
group by group
having x=max(x);
quit;

Now tell us what if there are ties in X values?

tsap
Pyrite | Level 9
DATA WORK.HAVE;
FORMAT 	 Group $1. A 8. B 8. C 8. X 8.;
INFORMAT Group $1. A 8. B 8. C 8. X 8.;
INPUT    Group 	   A 	B 	 C 	  X;
INFILE DATALINES DLM='|' DSD;
DATALINES;
1|5|3|1|12
1|6|4|1|9
1|7|6|3|10
2|4|2|8|7
2|3|7|5|8
2|5|2|4|13
3|2|10|5|9
3|4|9|3|15
3|7|6|7|14
;

PROC SORT DATA=WORK.HAVE; BY GROUP X; RUN;

DATA WORK.WANT;
	SET WORK.HAVE;
	BY GROUP X;
	IF LAST.GROUP AND LAST.X;
RUN;

 

By sorting the data by 'Group' and 'X', in the following datastep we can utilize the 'last.variable' feature. So the last iteration within each unique group id with the highest 'X' value within that unique group id.

 

Output:

Group	A	B	C	X
1	5	3	1	12
2	5	2	4	13
3	4	9	3	15
antor82
Obsidian | Level 7

tsk so much!

 

 

works perfectly

tsap
Pyrite | Level 9
You're welcome. Glad I could help.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1744 views
  • 0 likes
  • 3 in conversation