BookmarkSubscribeRSS Feed
don21
Quartz | Level 8

Dear all,

 

I have been facing an issue with one logic, I have a list of dates and amounts, I need to pick the max amount from a same date based on a name.. attached is the example. kindly help in finding the mistake here.. Thank you..

 

 

 

4 REPLIES 4
gamotte
Rhodochrosite | Level 12

Hello,

 

data have;
    infile cards dlm=',' dsd;
    attrib Date format=date9. informat=ddmmyy8.;
    length name $10.;
    input ID Date name $ Amount Flag_first_name Flag_last_name;
cards;
12345,15082014,Bob,100,0,0
12345,20122016,Bob,200,0,0
12345,20122016,Bob,150,0,1
67890,31012012,John,100,0,0
67890,31012006,John,200,1,0
;
run;

proc sql;
    CREATE TABLE want AS
    SELECT *
    FROM have
    WHERE strip(name)="Bob"
    GROUP BY name, date
    HAVING Amount=max(Amount);
quit;
don21
Quartz | Level 8

Hi Gamotte,

 

Your below program will not work when there is a case like 

 

Bob  21032016  200

Bob  21032016  300

Bob  31122014   500

 

in this case, your logic will return 500, but the answer I am looking for is 200,

 

requirement is , when name=bob, date is latest (when latest data is same and has multiple records, then it should return max amount from those).. so the answer should be Bob  21032016  200

 

kindly advise.

gamotte
Rhodochrosite | Level 12

I am not sure i understand the logic you want to implement.

From your description, i would guess the answer should be 300 and not 200.

 

 

data have;
    infile cards dlm=',' dsd;
    attrib Date format=date9. informat=ddmmyy8.;
    length name $10.;
    input ID Date name $ Amount Flag_first_name Flag_last_name;
cards;
12345,21032016,Bob,200,0,0
12345,21032016,Bob,300,0,0
12345,31122014,Bob,500,0,0
67890,31012012,John,100,0,0
67890,31012006,John,200,1,0
;
run;

proc sql;
    CREATE TABLE want AS
	SELECT * FROM (
        SELECT *
        FROM have
        WHERE strip(name)="Bob"
        GROUP BY name
        HAVING Date=max(Date)
	)
    GROUP BY Name
    HAVING Amount=max(Amount);
quit;
Kurt_Bremser
Super User

Stealing @gamotte's example data:

data have;
    infile cards dlm=',' dsd;
    attrib Date format=date9. informat=ddmmyy8.;
    length name $10.;
    input ID Date name $ Amount Flag_first_name Flag_last_name;
cards;
12345,21032016,Bob,200,0,0
12345,21032016,Bob,300,0,0
12345,31122014,Bob,500,0,0
67890,31012012,John,100,0,0
67890,31012006,John,200,1,0
;
run;

proc sort data=have;
by id date amount;
run;

data want;
set have;
by id;
if last.id;
run;

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