Hi Guys, can help me with the following scenerio below. I need to derive the number of days for DAY_LAST_TRNS field. If there is a transaction taken place, the calculation of day_last trns field will be MTH_END - TRNS_DTE. However, if there was a refund to net off the previous transaction, we will not consider it as a transaction record. Therefore, we will take the last transaction as the latest record. Please advice what is the code to derive for DAY_LAST_TRNS.
MTH END | TRNS_DTE | NAME | TRNF_AMT | DAY_LAST_TRNS (MTH_END-TRNS_DTE) | |
31May2001 | 04May2001 | ABC | 10000.000000 | 27 | |
31Jan2012 | 17Jan2012 | ABC | 3564.000000 | 14 | |
31Jan2013 | 30Jan2013 | ABC | 3564.000000 | 1 | |
31Dec2013 | 31Dec2013 | ABC | 4000.000000 | 0 | |
31Jan2015 | 19Jan2015 | ABC | 4000.000000 | 12 | |
29Feb2016 | 16Feb2016 | ABC | 4000.000000 | 13 | |
28Feb2017 | 15Feb2017 | ABC | 4000.000000 | 13 | |
28Feb2018 | 03Feb2018 | ABC | 2000.000000 | 25 | |
31Jul2018 | 27Jul2018 | ABC | 2000.000000 | 4 | |
31Jan2019 | 25Jan2019 | ABC | 2000.000000 | 6 | |
30Sep2019 | 02Sep2019 | ABC | 5300.000000 | 28 | |
30Sep2019 | 02Sep2019 | ABC | -5300.000000 | 248 | Days = 30 Sept - 25 Jan |
A couple of questions:
Do refund records always immediately follow the refunded transaction? Or can there be other transactions between them?
I think you are saying:
If your date variables (per @PaigeMiller's question) are numeric data values, and if all refunds immediately follow the corresponding transaction (my question), then you can keep track of an intermediate variable (_last_non_refund_date), which you retain across observations. If the current transaction is not a refund, use TRNS_DATE. But if it is, use _last_non_refund_date:
data have;
input MTH_END :date9. TRNS_DTE :date9. NAME :$3. TRNF_AMT expected_DAY_LAST_TRNS ;
format trns_dte mth_end date9.;
datalines;
31May2001 04May2001 ABC 10000.000000 27
31Jan2012 17Jan2012 ABC 3564.000000 14
31Jan2013 30Jan2013 ABC 3564.000000 1
31Dec2013 31Dec2013 ABC 4000.000000 0
31Jan2015 19Jan2015 ABC 4000.000000 12
29Feb2016 16Feb2016 ABC 4000.000000 13
28Feb2017 15Feb2017 ABC 4000.000000 13
28Feb2018 03Feb2018 ABC 2000.000000 25
31Jul2018 27Jul2018 ABC 2000.000000 4
31Jan2019 25Jan2019 ABC 2000.000000 6
30Sep2019 02Sep2019 ABC 5300.000000 28
30Sep2019 02Sep2019 ABC -5300.000000 248
run;
data want (drop=_:);
set have (keep=name);
by name;
merge have
have (firstobs=2 keep=trnf_amt rename=(trnf_amt=_nxt_trnf_amt));
retain _last_non_ref_date .;
if first.name then _last_non_ref_date= .;
if trnf_amt>0 and _nxt_trnf_amt>0 then _last_non_ref_date=trns_dte;
new_DAY_LAST_TRNS = mth_end - ifn(trnf_amt>0,trns_dte,_last_non_ref_date);
run;
Unresolved: what do you want if the very first transaction is refunded in the second transaction? In my program the resulting DAYS_LST_TRNS is a missing value for the refund transaction.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.