BookmarkSubscribeRSS Feed
dhallkaran
Calcite | Level 5

Hi community members,

 

I have a data set that has credit card transactions for accounts through time.

 

HaveHaveHaveRequiredRequired
AccountCredit _LimitTransaction_amountDecisionOTB_remaining
11000500Approve500
11000900Decline500
11000400Approve100
220001000Approve1000
220001500Decline1000
220002000Decline1000
220001000Approve0

 

Have  are the variables that are existing in the data set.

Required  are the variables that I need to create.

 

Example Account#1:

 

1. As the first transaction for account#1 is for $500 and the credit limit is $1000, the transaction should be approved and the Open To Buy(OTB) should be Credit Limit ($1000)-Transaction Amount($500)=$500.

 

2. Now for next transaction as $500 is available for spend, and the customer tries a spend of $900, the transaction should be declined and $500 should be OTB available for next transaction.

 

3. As the 3rd transaction is for $400 and $500 is available to spend, the transaction should be approved and OTB should be updated to $100.

 

The same should apply to Account#2.

 

As this requires holding previous values of OTB and decision to make subsequent decisions, I am having a hard time figuring out how to code it in SAS.

 

Any help here is much appreciated!

 

 

4 REPLIES 4
yabwon
Amethyst | Level 16

Hi,

 

This seems to be doing the job:

data have;
input Account	Credit_Limit Transaction_amount; /* Decision OTB_remaining */
cards;
1 1000 500 Approve	500
1 1000 900 Decline	500
1 1000 400 Approve	100
2 2000 1000 Approve	1000
2 2000 1500 Decline	1000
2 2000 2000 Decline	1000
2 2000 1000 Approve	0
;
run;



data want;
  set have;
  by Account;

  /* current limit on the account */
  retain  current_CL 0;
  if first.account then current_CL = Credit_Limit;
  drop current_CL ;

  /* test for credit limit */
  if Transaction_amount <= current_CL then
    do;
     Decision = "Approve";
     OTB_remaining = current_CL - Transaction_amount;  
    end;
  else
    do;
     Decision = "Decline";
     OTB_remaining = current_CL;
    end;

  output;
  current_CL = OTB_remaining;
run;

proc print;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



novinosrin
Tourmaline | Level 20

data have;
input Account	Credit_Limit	Transaction_amount;*	Decision	OTB_remaining;
cards;
1	1000	500	Approve	500
1	1000	900	Decline	500
1	1000	400	Approve	100
2	2000	1000	Approve	1000
2	2000	1500	Decline	1000
2	2000	2000	Decline	1000
2	2000	1000	Approve	0
;




data want;
 do until(last.account);
  set have;
  by account;
  if first.account then OTB_remaining=Credit_Limit;
  _n_=OTB_remaining-Transaction_amount;
  if sign(_n_)=-1 then Decision='Decline';
  else do;
   Decision='Approve';
   OTB_remaining=_n_;
  end;
  output;
 end;
run;
mkeintz
PROC Star

I think @yabwon 's response clarifies the logic nicely.  This program is a little different - instead of checking a condition and then making two modifications (decision and otb_remaining), this checks the condition and then makes one modification (decision).  Then it uses decision to conditionally make the second modification.

 

data have;
input Account	Credit_Limit Transaction_amount; /* Decision OTB_remaining */
cards;
1 1000 500 Approve	500
1 1000 900 Decline	500
1 1000 400 Approve	100
2 2000 1000 Approve	1000
2 2000 1500 Decline	1000
2 2000 2000 Decline	1000
2 2000 1000 Approve	0
;
run;

data want;
  set have;
  by account;

length decision $8; if first.account then otb_remaining=credit_limit; decision=ifc(transaction_amount<=otb_remaining,'accept','decline'); if decision='accept' then otb_remaining+ -1*transaction_amount; run;

There's also a non-obvious "summing statement" above (a summing statement is like   "x+a;" instead of "x=x+a;").  When a variable is the outcome of a summing statement, it is automatically retained.

 

The summing statement here is conditionally executed in the "if decision='accept' test.  And notice I had to change its syntax to

     otb_remaining+ -1*transaction_amount;

since I needed to subtract, not add - while preserving the recognizable syntax of a summing statement (must start with "X+ ...").

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
data have;
input Account	Credit_Limit Transaction_amount; 
cards;
1 1000 500 Approve	500
1 1000 900 Decline	500
1 1000 400 Approve	100
2 2000 1000 Approve	1000
2 2000 1500 Decline	1000
2 2000 2000 Decline	1000
2 2000 1000 Approve	0
;
run;
data want;
 set have;
 by Account;
 if first.Account then cum=0;
 cum+Transaction_amount;
 _Transaction_amoun=Transaction_amount;
 if cum<=Credit_Limit then Decision='Approve  ';
  else do;
    cum=cum-_Transaction_amoun;
	Decision='Decline  ';
  end;
  OTB_remaining=Credit_Limit-cum;
drop _Transaction_amoun cum;
run;
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1171 views
  • 2 likes
  • 5 in conversation