<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Iteration through observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Iteration-through-observations/m-p/672851#M202267</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This seems to be doing the job:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;lt;= 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;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Bart&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Jul 2020 14:31:46 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2020-07-28T14:31:46Z</dc:date>
    <item>
      <title>Iteration through observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iteration-through-observations/m-p/672837#M202263</link>
      <description>&lt;P&gt;Hi community members,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set that has credit card transactions for accounts through time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;Have&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;Have&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;Have&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;U&gt;&lt;STRONG&gt;Required&lt;/STRONG&gt;&lt;/U&gt;&lt;/TD&gt;&lt;TD&gt;&lt;U&gt;&lt;STRONG&gt;Required&lt;/STRONG&gt;&lt;/U&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;Account&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;Credit _Limit&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;Transaction_amount&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;U&gt;&lt;STRONG&gt;Decision&lt;/STRONG&gt;&lt;/U&gt;&lt;/TD&gt;&lt;TD&gt;&lt;U&gt;&lt;STRONG&gt;OTB_remaining&lt;/STRONG&gt;&lt;/U&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;1000&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;500&lt;/STRONG&gt;&lt;/EM&gt;&lt;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t;EM&gt;&lt;STRONG&gt;2000&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;1000&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;U&gt;&lt;STRONG&gt;Approve&lt;/STRONG&gt;&lt;/U&gt;&lt;/TD&gt;&lt;TD&gt;&lt;U&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/U&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Have&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt; are the variables that are existing in the data set.&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Required&amp;nbsp;&lt;/STRONG&gt;&lt;/U&gt; are the variables that I need to create.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example Account#1:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The same should apply to Account#2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help here is much appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 14:06:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iteration-through-observations/m-p/672837#M202263</guid>
      <dc:creator>dhallkaran</dc:creator>
      <dc:date>2020-07-28T14:06:40Z</dc:date>
    </item>
    <item>
      <title>Re: Iteration through observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iteration-through-observations/m-p/672851#M202267</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This seems to be doing the job:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;lt;= 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;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Bart&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 14:31:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iteration-through-observations/m-p/672851#M202267</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-07-28T14:31:46Z</dc:date>
    </item>
    <item>
      <title>Re: Iteration through observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iteration-through-observations/m-p/672854#M202269</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Jul 2020 14:32:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iteration-through-observations/m-p/672854#M202269</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-07-28T14:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: Iteration through observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iteration-through-observations/m-p/672860#M202271</link>
      <description>&lt;P&gt;I think &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt; 's response clarifies the logic nicely.&amp;nbsp; 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).&amp;nbsp; Then it uses decision to conditionally make the second modification.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;
&lt;BR /&gt;  length decision $8;
  if first.account then otb_remaining=credit_limit;
  decision=ifc(transaction_amount&amp;lt;=otb_remaining,'accept','decline');
  if decision='accept' then otb_remaining+ -1*transaction_amount;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There's also a non-obvious "summing statement" above (a summing statement is like&amp;nbsp;&amp;nbsp; "x+a;" instead of "x=x+a;").&amp;nbsp; When a variable is the outcome of a summing statement, it is automatically retained.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The summing statement here is conditionally executed in the "if decision='accept' test.&amp;nbsp; And notice I had to change its syntax to&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; otb_remaining+ -1*transaction_amount;&lt;/P&gt;
&lt;P&gt;since I needed to subtract, not add - while preserving the recognizable syntax of a summing statement (must start with "X+ ...").&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 15:12:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iteration-through-observations/m-p/672860#M202271</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-28T15:12:56Z</dc:date>
    </item>
    <item>
      <title>Re: Iteration through observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iteration-through-observations/m-p/673097#M202423</link>
      <description>&lt;PRE&gt;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&amp;lt;=Credit_Limit then Decision='Approve  ';
  else do;
    cum=cum-_Transaction_amoun;
	Decision='Decline  ';
  end;
  OTB_remaining=Credit_Limit-cum;
drop _Transaction_amoun cum;
run;
&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jul 2020 12:39:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iteration-through-observations/m-p/673097#M202423</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-07-29T12:39:00Z</dc:date>
    </item>
  </channel>
</rss>

