BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

I wish to know the way to find out the maximum number of sales amount by each salesperson via datastep.

 

I've a data like this

 

Salesid     Sales_amount        Salesperson

001           100                         Dean

001           200                         Dean

002           300                         Tom

002           700                         Tom

003           500                         Jacob

003           400                         Jacob

 

Desired output should be like this

 

Salesid     Sales_amount        Salesperson

001           200                         Dean

002           700                         Tom

003           500                         Jacob

 

Thanks for any help you offer me.

 

3 REPLIES 3
Reeza
Super User

Sort descending and then take the first record for the table.

 

proc sort data=have;
by salesid descending sales_amount;
run;

data want;
set have;
by salesid;
if first.salesid;
run;
FreelanceReinh
Jade | Level 19

Hi @Babloo,

 

Alternatively, you could use a double DOW loop:

data have;
input Salesid $ Sales_amount Salesperson $;
cards;
001 100 Dean
001 200 Dean
002 300 Tom
002 700 Tom
003 500 Jacob
003 400 Jacob
;

data want;
do until(last.salesid);
  set have;
  by salesid;
  maxamt=max(maxamt,sales_amount);
end;
do until(last.salesid);
  set have;
  by salesid;
end;
drop sales_amount;
rename maxamt=Sales_amount;
run;

proc print data=want noobs;
var salesid sales_amount salesperson;
run;
Astounding
PROC Star

Just curious ... why are you insisting on a DATA step solution?  Both PROC SQL and PROC SUMMARY can easily handle this without the need to sort your data.

sas-innovate-2024.png

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.

 

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
  • 3 replies
  • 992 views
  • 3 likes
  • 4 in conversation