BookmarkSubscribeRSS Feed
JT99
Obsidian | Level 7
Hi guys! How do I get the latest payment of an account with multiple payments on the same date? I need the sum of payments if seperate payments are done on the same date.

Acct_num Payment Pymnt_date
1 21 1oct2015
1 10 1oct2015
1 12 29sep2015
2 15 5oct201
2 11 3oct2015

Latest payment of acct_num 1 should be 31 and 15 for acct_num 2.
5 REPLIES 5
slchen
Lapis Lazuli | Level 10


proc sql;
select acct_num,sum(payment) as sum
from (select acct_num,payment,pymnt_date
from have group by acct_num
having pymnt_date=max(pymnt_date))
group by acct_num;
quit;

billfish
Quartz | Level 8

*** a non SQL solution ****;

 

data t_have;
input Acct_num Payment Pymnt_date date9.;
cards;
1 21 1oct2015
1 10 1oct2015
1 12 29sep2015
2 15 5oct2015
2 11 3oct2015
;
run;

proc print data=t_a;
format Pymnt_date date9.;
run;

proc sort data=t_have; by acct_num descending Pymnt_date; run;

data t_want(keep=acct_num last_date sum_tot);
  set t_have;
  retain sum_tot last_date;
  by acct_num;
  if first.acct_num then do; sum_tot=0; last_date=Pymnt_date; end;
    if (Pymnt_date= last_date) then sum_tot+payment;
  if last.acct_num then output;
run;

proc print data=t_want;
 format last_date date9.;
run;

Astounding
PROC Star

Why do identical amounts on the same day (but with different transaction numbers) represent the same transaction?  Couldn't a customer change his mind and go back and buy another of the same item (or perhaps another item with the same price)?  Couldn't a refund be for part of a purchase instead of the entire purchase?

JT99
Obsidian | Level 7
What I meant was multiple payments to the same account on the same day. Like when you pay your elec bill twice on the same day.
Reeza
Super User
You could also do a proc means by acct_num and pymt_date. Then calculate 2 metrics, total payment and number of payments per date.

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
  • 1410 views
  • 0 likes
  • 5 in conversation