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

Hi I have two tables in sas,(acct_details table) with columns acct_no ,act_status and  yearmonth

Table B( payment_trns table) has columns: yearmonth , trns_type,  acct_no , and eff_date

 

I need to fetch the first transaction from payment_trns(Only the trns_type, eff_date columns) done in the same period the account was re-activated. I created the logic on the provided code  below code but i get incorrect results on the screenshot below the code 

 

data acct_details;
    format act_status $12.;
    input acct_no act_status $ yearmonth;
    datalines;
101 reactivated 202301
102 active 202301
103 reactivated 202302
103 reactivated 202303
;
run;

data payment_trns;
    informat EFF_DATE date9.;
    format EFF_DATE date9.;
    input yearmonth trns_type $ acct_no eff_date;
    datalines;
202301 TypeA 101 05JAN2023
202301 TypeB 101 10JAN2023
202302 TypeC 101 15FEB2023
202301 TypeA 102 07JAN2023
202301 TypeB 102 12JAN2023
202302 TypeA 103 03FEB2023
202302 TypeB 103 10FEB2023
202303 TypeD 103 11MAR2023
;
run;


proc sql;
    create table final_table as
    select A.*,
           B.eff_date,
           B.trns_type
    from acct_details as A
    left join (
        select yearmonth,
               acct_no,
               min(eff_date) as first_eff_date
        from payment_trns
        group by yearmonth, acct_no
    ) as  B_temp
    on A.yearmonth = B_temp.yearmonth and A.acct_no = B_temp.acct_no
    left join payment_trns as B
    on B_temp.yearmonth = B.yearmonth and B_temp.acct_no = B.acct_no 
    where A.act_status = 'reactivated';
quit;

Solly7_0-1692278357298.png

 

 

Data want

act_status      acct_no      yearmonth  EFF_DATE     trns_type
reactivated    101                202301        15JAN2023     TypeC
reactivated   103                202302       03FEB2023     TypeA
reactivated   103                202303      11MAR2023     TypeD

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
proc sql;
create table want as
  select
    a.*,
    b.eff_date,
    b.trns_date
  from acct_details a left join payment_trns b
  on a.acct_no = b.acct_no and a.yearmonth = b.yearmonth and a.acct_status = "reactivated"
  group by a.acct_no, a.yearmonth
  having b.eff_date = min(b.eff_date)
;
quit;

Untested, posted from my tablet.

View solution in original post

3 REPLIES 3
Solly7
Pyrite | Level 9

see above corrected desired results

Data want

act_status      acct_no      yearmonth  EFF_DATE     trns_type
reactivated    101                202301        05JAN2023     TypeC
reactivated   103                202302       03FEB2023     TypeA
reactivated   103                202303      11MAR2023     TypeD

Kurt_Bremser
Super User
proc sql;
create table want as
  select
    a.*,
    b.eff_date,
    b.trns_date
  from acct_details a left join payment_trns b
  on a.acct_no = b.acct_no and a.yearmonth = b.yearmonth and a.acct_status = "reactivated"
  group by a.acct_no, a.yearmonth
  having b.eff_date = min(b.eff_date)
;
quit;

Untested, posted from my tablet.

Solly7
Pyrite | Level 9

Thanks a lot @Kurt_Bremser , it works i just changed 

and a.acct_status = "reactivated"

to 

where a.acct_status = "reactivated"

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 445 views
  • 1 like
  • 2 in conversation