<?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: HOW TO CHECK PREVIOUS ROW IS POSITIVE OR NEGATIVE in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/HOW-TO-CHECK-PREVIOUS-ROW-IS-POSITIVE-OR-NEGATIVE/m-p/749128#M235345</link>
    <description>&lt;P&gt;This example code with the data you have provided, helps do what you wanted.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
length particulars $ 50;
retain sm;
input particulars $ 	amount;
infiles datalines dsd dlm=',' missover;
if amount &amp;lt;0 then status='Y';
datalines;
Amt to be received' as on 01 JUN 2021,	0
Amt  to be received' as on 02 JUN 2021,	200
Amt to be received' as on 03 JUN 2021,	-300
Amt to be received' as on 04 JUN 2021,	-400
Amt to be received' as on 05 JUN 2021,	-500
;
run;
proc sql;
create table temp as
select "Total" as Particulars Length= $ 50, sum(amount)*(-1) as amount from test 
where status='Y';
quit;
proc append base=test(drop=status) data=temp Force;
run;
data test (drop=Status);
set test;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output will be like this&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_0-1624203670856.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60539i68037801825D18CA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sajid01_0-1624203670856.png" alt="Sajid01_0-1624203670856.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 20 Jun 2021 15:41:25 GMT</pubDate>
    <dc:creator>Sajid01</dc:creator>
    <dc:date>2021-06-20T15:41:25Z</dc:date>
    <item>
      <title>HOW TO CHECK PREVIOUS ROW IS POSITIVE OR NEGATIVE</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HOW-TO-CHECK-PREVIOUS-ROW-IS-POSITIVE-OR-NEGATIVE/m-p/749108#M235339</link>
      <description>&lt;P&gt;I had a input like below .Suppose if todays date is june5th2021 and the amount is -500 . I need to check the previous day balance&amp;nbsp; and if the previous days balance(4thjune2021) is also a negative value then again i need to check the before previous day balance(3thjune2021) till the positive balance value is reflected .&lt;/P&gt;
&lt;P&gt;Finally the total should be sum of all previous days negative balance till the positive value appears&amp;nbsp; reflected as like in the below table marked in RED .&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;TABLE width="392"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="294"&gt;particulars&amp;nbsp;&lt;/TD&gt;
&lt;TD width="98"&gt;amount&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Amt to be received' as on 01 JUN 2021&lt;/TD&gt;
&lt;TD&gt;-300&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Amt&amp;nbsp; to be received' as on 02 JUN 2021&lt;/TD&gt;
&lt;TD&gt;200&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;FONT color="#808000"&gt;Amt to be received' as on 03 JUN 2021&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT color="#808000"&gt;-300&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;FONT color="#808000"&gt;Amt to be received' as on 04 JUN 2021&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT color="#808000"&gt;-400&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;FONT color="#808000"&gt;Amt to be received' as on 05 JUN 2021&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT color="#808000"&gt;-500&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;FONT color="#FF0000"&gt;Total&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;FONT color="#FF0000"&gt;1200&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please help me .Thanks in advance..!!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Jun 2021 17:07:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HOW-TO-CHECK-PREVIOUS-ROW-IS-POSITIVE-OR-NEGATIVE/m-p/749108#M235339</guid>
      <dc:creator>rohithverma</dc:creator>
      <dc:date>2021-06-20T17:07:11Z</dc:date>
    </item>
    <item>
      <title>Re: HOW TO CHECK PREVIOUS ROW IS POSITIVE OR NEGATIVE</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HOW-TO-CHECK-PREVIOUS-ROW-IS-POSITIVE-OR-NEGATIVE/m-p/749128#M235345</link>
      <description>&lt;P&gt;This example code with the data you have provided, helps do what you wanted.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
length particulars $ 50;
retain sm;
input particulars $ 	amount;
infiles datalines dsd dlm=',' missover;
if amount &amp;lt;0 then status='Y';
datalines;
Amt to be received' as on 01 JUN 2021,	0
Amt  to be received' as on 02 JUN 2021,	200
Amt to be received' as on 03 JUN 2021,	-300
Amt to be received' as on 04 JUN 2021,	-400
Amt to be received' as on 05 JUN 2021,	-500
;
run;
proc sql;
create table temp as
select "Total" as Particulars Length= $ 50, sum(amount)*(-1) as amount from test 
where status='Y';
quit;
proc append base=test(drop=status) data=temp Force;
run;
data test (drop=Status);
set test;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output will be like this&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_0-1624203670856.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60539i68037801825D18CA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sajid01_0-1624203670856.png" alt="Sajid01_0-1624203670856.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Jun 2021 15:41:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HOW-TO-CHECK-PREVIOUS-ROW-IS-POSITIVE-OR-NEGATIVE/m-p/749128#M235345</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-06-20T15:41:25Z</dc:date>
    </item>
    <item>
      <title>Re: HOW TO CHECK PREVIOUS ROW IS POSITIVE OR NEGATIVE</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HOW-TO-CHECK-PREVIOUS-ROW-IS-POSITIVE-OR-NEGATIVE/m-p/749137#M235350</link>
      <description>Thanks for your reply .But as per UPDATED requirement shown below table the sum should be still 1200 &lt;BR /&gt;&lt;BR /&gt;particulars 	amount&lt;BR /&gt;Amt to be received' as on 01 JUN 2021	-300&lt;BR /&gt;Amt  to be received' as on 02 JUN 2021	200&lt;BR /&gt;Amt to be received' as on 03 JUN 2021	-300&lt;BR /&gt;Amt to be received' as on 04 JUN 2021	-400&lt;BR /&gt;Amt to be received' as on 05 JUN 2021	-500&lt;BR /&gt;Total	1200&lt;BR /&gt;</description>
      <pubDate>Sun, 20 Jun 2021 17:06:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HOW-TO-CHECK-PREVIOUS-ROW-IS-POSITIVE-OR-NEGATIVE/m-p/749137#M235350</guid>
      <dc:creator>rohithverma</dc:creator>
      <dc:date>2021-06-20T17:06:32Z</dc:date>
    </item>
    <item>
      <title>Re: HOW TO CHECK PREVIOUS ROW IS POSITIVE OR NEGATIVE</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HOW-TO-CHECK-PREVIOUS-ROW-IS-POSITIVE-OR-NEGATIVE/m-p/749139#M235351</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With a DATA step, calculating that sort of running total is straight forward.&amp;nbsp; You need to use the RETAIN statement&amp;nbsp; to retain the value of accumulator variable.&amp;nbsp; The SUM statement implicitly retains an accumulator as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input amount ;
  cards ;
-300
200
-300
-400
-500 
;
run ;

data want ;
  set have ;

  if amount&amp;lt;0 then Total+abs(Amount) ; *Total is an accumulator variable;
  else Total=0 ; *Reset total to 0 whenever Amount is &amp;gt;0;

  put (_n_ amount total)(=) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Jun 2021 17:22:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HOW-TO-CHECK-PREVIOUS-ROW-IS-POSITIVE-OR-NEGATIVE/m-p/749139#M235351</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2021-06-20T17:22:58Z</dc:date>
    </item>
  </channel>
</rss>

