BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Solly7
Pyrite | Level 9

Hi i need help, i have two table below, table1 is payment_transactions( consisting of all collections transactions), and the other table is ITC_data(consisting of credit score and lapse score). all i need is every record from payment_transactions table with itc_score and lapse_scores(date_received) <= trans_date for each record. If particular policy number have two received dates<=action_date, i need to take one(latest  record) to avoid duplicates

 

 

                                                                          Data Have

                table1(payment_transactions table)                                             table2(ITC_data)                            

policy_no        trans_date          amount                                policy_no     date_received       lapse_score      itc_score

1                    01Jan2019               500                                       1                 01Dec2018            50                  500

1                    01Feb2019               500                                       2                 01Jan2019             10                 700

2                    30Jan2019               300                                       2                 15Jan2019             30                 610 

2                    30Feb2019               300                                      

                              

 

                                                                        Data Want

policy_no        trans_date          amount        date_received       lapse_score       itc_score

1                    01Jan2019               500            01Dec2018                 50                500        

1                    01Feb2019               500            01Dec2018                50                500

2                    30Jan2019               300            15Jan2019                 30                610               

2                    30Feb2019               300            15Jan2019                 30               610

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Everything in one SQL:

proc sql;
create table want as
  select
    t1.policy_no,
    t1.trans_date,
    t1.amount,
    t2.date_received as date_received,
    t2.lapse_score,
    t2.itc_score
  from table1 t1 left join table2 t2
  on t1.policy_no = t2.policy_no and t2.date_received <= t1.trans_date
  group by t1.policy_no, t1.trans_date
  having t2.date_received = max(t2.date_received)
;
quit;

View solution in original post

8 REPLIES 8
Solly7
Pyrite | Level 9
Hi Kurt, i simply mean date_received<=trans_date
PaigeMiller
Diamond | Level 26

UNTESTED CODE

 

proc sql;
     create table want as select table1.*,table2.date_received,table2.lapse_score,
         table2.itc_score
     from table1 left join table2 
    on table1.policy_no=table2.policy_no
    group by table2.policy_no
    having table2.date_received = max(table2.date_received);
quit;

What do you mean by "action_date"?

--
Paige Miller
Solly7
Pyrite | Level 9
Hey Paige Miller, apologies i meant trans_date
Kurt_Bremser
Super User

And you won't find 30Feb2019 in any calendar I know of.

Please supply example data in WORKING(!!) data steps with datalines, as by doing this you yourself can find such mistakes on your own.

Jagadishkatam
Amethyst | Level 16

please try the below code

 

data have1;
input policy_no trans_date:date9. amount;
format trans_date date9.;
cards;                          
1 01Jan2019 500                       
1 01Feb2019 500                       
2 30Jan2019 300                       
2 28Feb2019 300  
;     

data have2;
input policy_no date_received:Date9. lapse_score itc_score;
format date_received date9.;
cards;
1 01Dec2018 50 500               
2 01Jan2019 10 700               
2 15Jan2019 30 610     
;
          
proc sql;
create table want as select b.*, a.trans_date, a.amount from have1 as a left join have2 as b on a.policy_no=b.policy_no and b.date_received<= a.trans_date
order by b.policy_no, a.trans_date,b.date_received;
quit;

data want2;
set want;
by policy_no trans_date;
if last.trans_date;
run;
Thanks,
Jag
Kurt_Bremser
Super User

Everything in one SQL:

proc sql;
create table want as
  select
    t1.policy_no,
    t1.trans_date,
    t1.amount,
    t2.date_received as date_received,
    t2.lapse_score,
    t2.itc_score
  from table1 t1 left join table2 t2
  on t1.policy_no = t2.policy_no and t2.date_received <= t1.trans_date
  group by t1.policy_no, t1.trans_date
  having t2.date_received = max(t2.date_received)
;
quit;
Solly7
Pyrite | Level 9

Thanks a lot Kurt..its working perfectly fine

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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