Solved
Contributor
Posts: 74

# How to find out maximum value with group by condition ?

How to find out Maximum value with group by condition and below is example but it didnt worked :-

DATA VISITS;
INPUT ID VISIT_DATE : MMDDYY10. sell @@;
FORMAT VISIT_DATE DATE9.;
DATALINES;
1 02/01/2003 180 1 03/02/2003 178 1 04/01/2003 170
2 03/03/2003 170 2 04/01/2003 172
3 04/01/2003 130 3 06/01/2003 128 3 08/01/2003 128
;

proc sql;
select id,max(sum_sell) from
(select id ,sum(sell) as sum_sell from VISITS
group by id )
group by id;
quit;

Output :-

ID max_sell
------------------
1 528
2 342
3 386

but expected output is

ID max_sell
------------------
1 528

How to achieve this using proc sql ?

Accepted Solutions
Solution
‎03-13-2018 06:11 AM
Contributor
Posts: 74

## Re: How to find out maximum value with group by condition ?

DATA VISITS;
INPUT ID VISIT_DATE : MMDDYY10. sell @@;
FORMAT VISIT_DATE DATE9.;
DATALINES;
1 02/01/2003 180 1 03/02/2003 178 1 04/01/2003 170
2 03/03/2003 170 2 04/01/2003 172
3 04/01/2003 130 3 06/01/2003 128 3 08/01/2003 128
5 04/01/2003 130 5 06/01/2003 128 5 08/01/2003 128
5 04/01/2003 130 5 06/01/2003 128 5 08/01/2003 128
;

proc sql;
select * from (select id,sum(sell) as sum_sell from VISITS group by id ) where sum_sell =
(select
max(sum_sell) as sum_sell
from (select id,sum(sell) as sum_sell from VISITS group by id ) );
quit;

This is worked

All Replies
Super User
Posts: 5,849

## Re: How to find out maximum value with group by condition ?

Try using the having statement

``HAVING max(sum_sell) = sum_sell``
Data never sleeps
Contributor
Posts: 74

## Re: How to find out maximum value with group by condition ?

Still it didnt worked  :<

proc sql;
select id,
max(sum_sell)
from (select id,sum(sell) as sum_sell from VISITS group by id)
group by id
HAVING max(sum_sell) =sum_sell ;
quit;

output :-

ID
------------------
1 528
2 342
3 386

Super User
Posts: 9,402

## Re: How to find out maximum value with group by condition ?

The reason you get three records back is because:

```proc sql;
select id,         max(sum_sell)   from   (select id,sum(sell) as sum_sell from VISITS group by id)
group by id;
quit;```

You group the outside select clause by ID.  That means for each distinct ID a row will appear in the output, if you have ID's 1,2,3 then thte output will have rows for 1,2,3.  Do you want the max across all ID's?  If so then drop the last group by.  If you don't, then you have the correct result.

Solution
‎03-13-2018 06:11 AM
Contributor
Posts: 74

## Re: How to find out maximum value with group by condition ?

DATA VISITS;
INPUT ID VISIT_DATE : MMDDYY10. sell @@;
FORMAT VISIT_DATE DATE9.;
DATALINES;
1 02/01/2003 180 1 03/02/2003 178 1 04/01/2003 170
2 03/03/2003 170 2 04/01/2003 172
3 04/01/2003 130 3 06/01/2003 128 3 08/01/2003 128
5 04/01/2003 130 5 06/01/2003 128 5 08/01/2003 128
5 04/01/2003 130 5 06/01/2003 128 5 08/01/2003 128
;

proc sql;
select * from (select id,sum(sell) as sum_sell from VISITS group by id ) where sum_sell =
(select
max(sum_sell) as sum_sell
from (select id,sum(sell) as sum_sell from VISITS group by id ) );
quit;

This is worked

Super User
Posts: 9,402

## Re: How to find out maximum value with group by condition ?

And what did not work with the code I provided?  All you have done is wrap my code into a where clause (i.e. adding more processing time)?

☑ This topic is solved.

Discussion stats
• 5 replies
• 87 views
• 0 likes
• 3 in conversation