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 ?
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 🙂
Try using the having statement
HAVING max(sum_sell) = sum_sell
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
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.
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 🙂
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)?
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.