Thank you very much @novinosrin I made some changes but it is working out.
For a more dynamic, hash-based solution (requiring but a single pass over the input data set), look at the post by @novinosrin.
Below, essentially the same logic is executed using arrays. The first SQL data pass is used to size their high bound. Note that the Prior60 variable is extraneous (since all we need here is POST_DT), and so it's not used. I've also taken the liberty of abbreviating some variables and code True and False simply as T and F. Also note that -138061 is not some mystery number but merely the earliest possible SAS date value; it's used in the array D[0] item as a bulletproof sentinel.
data have ;
input acct group:$8. post_dt :date. bal fee ;
format POST_DT yymmdd10. ;
cards ;
1 ATM_WITH 26Sep2016 . 0.50
1 ATM_WITH 26Sep2016 . 0.50
1 ATM_WITH 26Sep2016 . 0.50
1 ATM_OD 30Sep2016 -150.14 36.00
1 ATM_WITH 06Oct2016 . 0.50
1 ATM_WITH 12Oct2016 . 0.50
1 ATM_WITH 26Oct2016 . 0.50
1 ATM_INQ 26Oct2016 . 0.50
1 ATM_OD 31Oct2016 -52.86 38.00
1 ATM_OD 14Nov2016 -187.87 38.00
1 ATM_OD 16Nov2016 -2.00 38.00
2 ATM_WITH 05Jan2017 . 0.50
2 ATM_INQ 13Jan2017 . 0.50
2 ATM_INQ 20Jan2017 . 0.50
2 ATM_OD 23Jan2017 -231.29 38.00
2 ATM_OD 24Jan2017 -493.54 38.00
2 ATM_OD 27Jan2017 -1.00 36.00
run ;
proc sql noprint ;
select max (q) into :hb from (select count(*) as q from have group acct) ;
quit ;
data want (drop = _:) ;
array d [0 : &hb] _temporary_ (-138061, &hb * 0) ;
array f [0 : &hb] _temporary_ ( 0, &hb * 0) ;
do _i = 1 by 1 until (last.acct) ;
set have ;
by acct ;
if group = "ATM_OD" then do ;
do _j = _i - 1 to 1 by -1 while (post_dt - d[_j] <= 60) ;
_fees = sum (_fees, f[_j]) ;
end ;
_tf = abs (bal) < _fees ;
flag = char ("FT", 1 + _tf) ;
fee_updt = ifn (_tf, fee, 0) ;
f[_i] = fee_updt ;
end ;
else f[_i] = fee ;
d[_i] = post_dt ;
output ;
call missing (fee_updt, flag) ;
end ;
run ;
Kind regards
Paul D.
@hashman aka Emperor of Hash and king of Paint-brush. I bow to you!
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.