BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MG18
Lapis Lazuli | Level 10

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 ? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
MG18
Lapis Lazuli | Level 10

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 🙂

 

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20

Try using the having statement

HAVING max(sum_sell) = sum_sell
Data never sleeps
MG18
Lapis Lazuli | Level 10

 

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

MG18
Lapis Lazuli | Level 10

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 🙂

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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)?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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