<?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: Cumulative sum/subtraction dependent on flag and account number in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561089#M10541</link>
    <description>&lt;P&gt;So in the first observation of a&amp;nbsp;&lt;SPAN&gt;TRA_RAC_ACCNO&lt;/SPAN&gt;&amp;nbsp;group new_bal will always be&amp;nbsp;&lt;SPAN&gt;ABA_BALANCE-TRANSAMOUNT, and in any subsequent observations it should be kept from the first.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;See this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input TransAmount Credit_Debit:$1. TRA_RAC_ACCNO $ ABA_BALANCE NEW_BAL;
label TransAmount="TransAmount" Credit_Debit="Credit-Debit" TRA_RAC_ACCNO="TRA_RAC_ACCN" ABA_BALANCE="ABA_BALANCE";
datalines;
110 C 500000 130.34 -6128.74
200 C 500006 502.78 -6328.74
83.07 C 500009 -1079.28 -6411.81
113.49 C 500026 112.63 -6525.3
39.74 C 500041 179.2 -6565.04
39.74 C 500041 179.2 -6604.78
80 C 500051 -959.93 -6684.78
35 C 500061 -210.58 -6719.78
57.83 C 500071 57.85 -6777.61
260.9 C 500080 319.52 -7038.51
6.82 C 500113 6.82 -7045.33
0.62 D 500132 -103.9 -7149.23
267.69 C 500161 203.7 -7416.92
98.96 C 500165 98.57 -7515.88
500 C 500175 -310.66 -8015.88
;

data want;
set have;
by TRA_RAC_ACCNO;
retain _new_bal;
if first.TRA_RAC_ACCNO
then do;
  new_bal = ABA_BALANCE - TRANSAMOUNT;
  _new_bal = new_bal;
end;
else new_bal = _new_bal;
drop _new_bal;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 23 May 2019 10:54:02 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-05-23T10:54:02Z</dc:date>
    <item>
      <title>Cumulative sum/subtraction dependent on flag and account number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561084#M10539</link>
      <description>&lt;P&gt;I have a data set that looks like the below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data WORK.TEST;&lt;BR /&gt;infile datalines dsd truncover;&lt;BR /&gt;input TransAmount:BEST12. Credit_Debit:$1. TRA_RAC_ACCNO:BEST12. ABA_BALANCE:BEST12. NEW_BAL:32.;&lt;BR /&gt;format TransAmount BEST12. TRA_RAC_ACCNO BEST12. ABA_BALANCE BEST12.;&lt;BR /&gt;label TransAmount="TransAmount" Credit_Debit="Credit-Debit" TRA_RAC_ACCNO="TRA_RAC_ACCN" ABA_BALANCE="ABA_BALANCE";&lt;BR /&gt;datalines;&lt;BR /&gt;110 C 500000 130.34 -6128.74&lt;BR /&gt;200 C 500006 502.78 -6328.74&lt;BR /&gt;83.07 C 500009 -1079.28 -6411.81&lt;BR /&gt;113.49 C 500026 112.63 -6525.3&lt;BR /&gt;39.74 C 500041 179.2 -6565.04&lt;BR /&gt;39.74 C 500041 179.2 -6604.78&lt;BR /&gt;80 C 500051 -959.93 -6684.78&lt;BR /&gt;35 C 500061 -210.58 -6719.78&lt;BR /&gt;57.83 C 500071 57.85 -6777.61&lt;BR /&gt;260.9 C 500080 319.52 -7038.51&lt;BR /&gt;6.82 C 500113 6.82 -7045.33&lt;BR /&gt;0.62 D 500132 -103.9 -7149.23&lt;BR /&gt;267.69 C 500161 203.7 -7416.92&lt;BR /&gt;98.96 C 500165 98.57 -7515.88&lt;BR /&gt;500 C 500175 -310.66 -8015.88&lt;BR /&gt;;;;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The New_Bal is being worked out wrong. If it's the first tra_rac_accno and the credit_debit is C then the New_Bal = ABA_BALANCE-TRANSAMOUNT&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the credit_debit is D then&amp;nbsp;New_Bal = ABA_BALANCE+TRANSAMOUNT&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For any other instance of tra_rac_accno then I want to use the previous New_Bal. So that it is a rolling balance with every transaction.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA TEST;
SET test;
IF FIRST.TRA_RAC_ACCNO THEN NEW_BAL = ABA_BALANCE;
IF CREDIT_DEBIT = 'C' THEN NEW_BAL=(NEW_BAL-TRANSAMOUNT);
ELSE IF CREDIT_DEBIT = 'D' THEN NEW_BAL + ABA_BALANCE;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my first time posting so please tell me if you need anymore information or if something isn't clear.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2019 12:47:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561084#M10539</guid>
      <dc:creator>manonlyn</dc:creator>
      <dc:date>2019-05-23T12:47:58Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum/subtraction dependent on flag and account number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561089#M10541</link>
      <description>&lt;P&gt;So in the first observation of a&amp;nbsp;&lt;SPAN&gt;TRA_RAC_ACCNO&lt;/SPAN&gt;&amp;nbsp;group new_bal will always be&amp;nbsp;&lt;SPAN&gt;ABA_BALANCE-TRANSAMOUNT, and in any subsequent observations it should be kept from the first.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;See this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input TransAmount Credit_Debit:$1. TRA_RAC_ACCNO $ ABA_BALANCE NEW_BAL;
label TransAmount="TransAmount" Credit_Debit="Credit-Debit" TRA_RAC_ACCNO="TRA_RAC_ACCN" ABA_BALANCE="ABA_BALANCE";
datalines;
110 C 500000 130.34 -6128.74
200 C 500006 502.78 -6328.74
83.07 C 500009 -1079.28 -6411.81
113.49 C 500026 112.63 -6525.3
39.74 C 500041 179.2 -6565.04
39.74 C 500041 179.2 -6604.78
80 C 500051 -959.93 -6684.78
35 C 500061 -210.58 -6719.78
57.83 C 500071 57.85 -6777.61
260.9 C 500080 319.52 -7038.51
6.82 C 500113 6.82 -7045.33
0.62 D 500132 -103.9 -7149.23
267.69 C 500161 203.7 -7416.92
98.96 C 500165 98.57 -7515.88
500 C 500175 -310.66 -8015.88
;

data want;
set have;
by TRA_RAC_ACCNO;
retain _new_bal;
if first.TRA_RAC_ACCNO
then do;
  new_bal = ABA_BALANCE - TRANSAMOUNT;
  _new_bal = new_bal;
end;
else new_bal = _new_bal;
drop _new_bal;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 May 2019 10:54:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561089#M10541</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-05-23T10:54:02Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum/subtraction dependent on flag and account number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561093#M10543</link>
      <description>Thanks for the answer, it doesn't address the Credit/Debit issue though. So I want it to take away the transamount if it's a credit and add on the transamount if it's a debit. Are you able to change it so that is taken into account?</description>
      <pubDate>Thu, 23 May 2019 11:03:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561093#M10543</guid>
      <dc:creator>manonlyn</dc:creator>
      <dc:date>2019-05-23T11:03:50Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum/subtraction dependent on flag and account number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561095#M10544</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/275242"&gt;@manonlyn&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks for the answer, it doesn't address the Credit/Debit issue though. So I want it to take away the transamount if it's a credit and add on the transamount if it's a debit. Are you able to change it so that is taken into account?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's not what you wrote. Quote from your original post:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;If it's the first tra_rac_accno and the credit_debit is C then the New_Bal = ABA_BALANCE-TRANSAMOUNT&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;If the credit_debit is D then&amp;nbsp;New_Bal = ABA_BALANCE-TRANSAMOUNT&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the formula is the same in both cases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the code needs a very simple extension:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by TRA_RAC_ACCNO;
retain _new_bal;
if first.TRA_RAC_ACCNO
then do;
  if credit_debit = 'C'
  then new_bal = ABA_BALANCE - TRANSAMOUNT;
  else new_bal = ABA_BALANCE + TRANSAMOUNT;
  _new_bal = new_bal;
end;
else new_bal = _new_bal;
drop _new_bal;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 May 2019 11:12:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561095#M10544</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-05-23T11:12:38Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum/subtraction dependent on flag and account number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561104#M10545</link>
      <description>&lt;P&gt;Sorry about that, it was a typo. Thanks for your reply. The only issue I'm having now is that if there is an Account with multiple transactions the New_Bal is repeated rather than worked out again.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;If you can see below where TRA_RAC_ACCNO = '500041' there are two transactions but the New_Bal is the same for both?&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;data WORK.TEST;&lt;BR /&gt;infile datalines dsd truncover;&lt;BR /&gt;input TransAmount:BEST12. Credit_Debit:$1. TRA_RAC_ACCNO:BEST12. ABA_BALANCE:BEST12. new_bal:32.;&lt;BR /&gt;format TransAmount BEST12. TRA_RAC_ACCNO BEST12. ABA_BALANCE BEST12.;&lt;BR /&gt;label TransAmount="TransAmount" Credit_Debit="Credit-Debit" TRA_RAC_ACCNO="TRA_RAC_ACCN" ABA_BALANCE="ABA_BALANCE";&lt;BR /&gt;datalines;&lt;BR /&gt;110 C 500000 130.34 20.34&lt;BR /&gt;200 C 500006 502.78 302.78&lt;BR /&gt;83.07 C 500009 -1079.28 -1162.35&lt;BR /&gt;113.49 C 500026 112.63 -0.86&lt;BR /&gt;39.74 C 500041 179.2 139.46&lt;BR /&gt;39.74 C 500041 179.2 139.46&lt;BR /&gt;80 C 500051 -959.93 -1039.93&lt;BR /&gt;35 C 500061 -210.58 -245.58&lt;BR /&gt;57.83 C 500071 57.85 0.02&lt;BR /&gt;260.9 C 500080 319.52 58.62&lt;BR /&gt;6.82 C 500113 6.82 0&lt;BR /&gt;0.62 D 500132 -103.9 -103.28&lt;BR /&gt;267.69 C 500161 203.7 -63.99&lt;BR /&gt;98.96 C 500165 98.57 -0.39&lt;BR /&gt;500 C 500175 -310.66 -810.66&lt;BR /&gt;;;;;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Thu, 23 May 2019 11:56:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561104#M10545</guid>
      <dc:creator>manonlyn</dc:creator>
      <dc:date>2019-05-23T11:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum/subtraction dependent on flag and account number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561118#M10547</link>
      <description>&lt;P&gt;Once again, quote from your OP:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;If it's the second tra_rac_accno then I want to use the New_Bal from the first.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Works as specified?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2019 12:31:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561118#M10547</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-05-23T12:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum/subtraction dependent on flag and account number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561125#M10550</link>
      <description>Sorry these are language issues as English is my second language. After discussing with a colleague what I meant was I want it to be a rolling balance, so using the one from before.</description>
      <pubDate>Thu, 23 May 2019 12:49:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561125#M10550</guid>
      <dc:creator>manonlyn</dc:creator>
      <dc:date>2019-05-23T12:49:12Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum/subtraction dependent on flag and account number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561128#M10551</link>
      <description>&lt;P&gt;That makes it even easier:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by TRA_RAC_ACCNO;
retain new_bal;
if first.TRA_RAC_ACCNO then new_bal = aba_balance;
new_bal + ifn(credit_debit = 'C',-TRANSAMOUNT,TRANSAMOUNT);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 May 2019 13:05:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum-subtraction-dependent-on-flag-and-account-number/m-p/561128#M10551</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-05-23T13:05:46Z</dc:date>
    </item>
  </channel>
</rss>

