<?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 calculate cumulative sum and exit do loop when condition is met in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-cumulative-sum-and-exit-do-loop-when-condition/m-p/444972#M111455</link>
    <description>&lt;P&gt;I was trying to simplify my question but I guess I didn't help myself. There is actually more than 1 terminal condition. Below is a version of my code. I tried to use %goto to exit out of iterating through the rest of a record when a terminal condition was met, but that didn't work for me. With the code below, I don't think the macro variable i is getting set right, because the code is creating perf_month_month5 and status_month5 when in fact it should stop at 4, and so once again the records are getting flagged incorrectly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need a macro do loop, where the loop is over 1 row, multiple columns, and not over multiple rows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; CalcRates(begdate, enddate);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; start=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%sysfunc&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(inputn(&amp;amp;begdate,anydtdte9.));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; end=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%sysfunc&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(inputn(&amp;amp;enddate,anydtdte9.));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;/*determines the number of months between the two dates*/&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; dif=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%sysfunc&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(intck(month,&amp;amp;start,&amp;amp;end));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%put&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; dif=&amp;amp;dif.;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Pmt_test;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;set Pmtraw&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;cumePmt=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; i=1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%do&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%while&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(&amp;amp;i&amp;lt;&amp;amp;dif);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;if Pmt_month&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;i.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; then Pmt_month&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;i.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;cumePmt = cumePmt + Pmt_month&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;i.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;if perf_month = &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;end.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; then do; &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="1"&gt;/* terminal condition 1*/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;flag = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="1"&gt;'Last cohort of ref data'&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; i=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%eval&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(&amp;amp;dif+1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;else if total &amp;lt;= &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; then do; &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="1"&gt;/* terminal condition 2*/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;flag = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="1"&gt;'Bal_at_obs eq 0'&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; i=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%eval&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(&amp;amp;dif+1); &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;else do;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;if strip(Status_month&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;i.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;) in (&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="1"&gt;'Closed'&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;) &lt;FONT color="#008000" face="Courier New" size="1"&gt;/* terminal condition 3*/&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then do;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;flag = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="1"&gt;'Closed in current month'&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; i=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%eval&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(&amp;amp;dif+1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;else if perf_month_month&amp;amp;i = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; then do; &lt;FONT color="#008000" face="Courier New" size="1"&gt;/* terminal condition 4*/&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;flag = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="1"&gt;'End of reference data'&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; i=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%eval&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(&amp;amp;dif+1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;else if cumePmt &amp;gt;= total then do; &lt;FONT color="#008000" face="Courier New" size="1"&gt;/* terminal condition 5*/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;flag = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="1"&gt;'Cumt Pmt &amp;gt; Bal'&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; i=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%eval&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(&amp;amp;dif+1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%end&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; CalcRates;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;%&lt;STRONG&gt;&lt;I&gt;CalcRates&lt;/I&gt;&lt;/STRONG&gt;(&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;31&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;JAN2014, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;31&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;MAY2014);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 12 Mar 2018 23:05:44 GMT</pubDate>
    <dc:creator>PS185</dc:creator>
    <dc:date>2018-03-12T23:05:44Z</dc:date>
    <item>
      <title>How to calculate cumulative sum and exit do loop when condition is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-cumulative-sum-and-exit-do-loop-when-condition/m-p/444949#M111448</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset where each row corresponds to 1 account with&amp;nbsp;multiple&amp;nbsp;dates and amounts. I want to calculate a cumulative amount per account, and exit iterating thought the rest of the dates when a certain condition is met.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a simple snapshot below, in this case I want to cumulate Amt1-Amtn as long as Statusn is not Closed. So I want the Cume Sum to be 25, not 40.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the %goto leave - but my code is still iterating through all the periods corresponding to a record, instead of exiting out of the do loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Acct&lt;/TD&gt;&lt;TD&gt;Perf1&lt;/TD&gt;&lt;TD&gt;Status1&lt;/TD&gt;&lt;TD&gt;Amt1&lt;/TD&gt;&lt;TD&gt;Perf2&lt;/TD&gt;&lt;TD&gt;Status2&lt;/TD&gt;&lt;TD&gt;Amt2&lt;/TD&gt;&lt;TD&gt;Perf3&lt;/TD&gt;&lt;TD&gt;Status3&lt;/TD&gt;&lt;TD&gt;Amt3&lt;/TD&gt;&lt;TD&gt;Cume Sum&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;Jan-14&lt;/TD&gt;&lt;TD&gt;Open&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;Feb-14&lt;/TD&gt;&lt;TD&gt;Open&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;Mar-14&lt;/TD&gt;&lt;TD&gt;Closed&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;25&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 12 Mar 2018 21:42:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-cumulative-sum-and-exit-do-loop-when-condition/m-p/444949#M111448</guid>
      <dc:creator>PS185</dc:creator>
      <dc:date>2018-03-12T21:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate cumulative sum and exit do loop when condition is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-cumulative-sum-and-exit-do-loop-when-condition/m-p/444954#M111450</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Acct	Perf1 $	Status1	$ Amt1	Perf2 $	Status2 $	Amt2	Perf3	$Status3 $	Amt3	;
datalines;
123	Jan-14	Open	10	Feb-14	Open	15	Mar-14	Closed	15	25
;

data want;
set have;
array s(*) status:;
array amt(*) amt:;
k=whichc('Closed',of s(*));
do _n_=1 to k-1;
cumu_sum=sum(cumu_sum,amt(_n_));
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Mar 2018 21:52:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-cumulative-sum-and-exit-do-loop-when-condition/m-p/444954#M111450</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-03-12T21:52:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate cumulative sum and exit do loop when condition is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-cumulative-sum-and-exit-do-loop-when-condition/m-p/444957#M111451</link>
      <description>&lt;P&gt;Mark&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;'s suggestion as the solution, but note that you don't need to create the counter variable K. You could just use:&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt; input Acct Perf1 $ Status1 $ Amt1 Perf2 $ Status2 $ Amt2 Perf3 $ Status3 $ Amt3 ;&lt;BR /&gt; datalines;&lt;BR /&gt;123 Jan-14 Open 10 Feb-14 Open 15 Mar-14 Closed 15 25&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt; set have;&lt;BR /&gt; array s(*) status:;&lt;BR /&gt; array amt(*) amt:;&lt;BR /&gt; do _n_=1 to whichc('Closed',of s(*))-1;&lt;BR /&gt; cumu_sum=sum(cumu_sum,amt(_n_));&lt;BR /&gt; end;&lt;BR /&gt;run;&lt;BR /&gt;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Mar 2018 22:04:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-cumulative-sum-and-exit-do-loop-when-condition/m-p/444957#M111451</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-03-12T22:04:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate cumulative sum and exit do loop when condition is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-cumulative-sum-and-exit-do-loop-when-condition/m-p/444972#M111455</link>
      <description>&lt;P&gt;I was trying to simplify my question but I guess I didn't help myself. There is actually more than 1 terminal condition. Below is a version of my code. I tried to use %goto to exit out of iterating through the rest of a record when a terminal condition was met, but that didn't work for me. With the code below, I don't think the macro variable i is getting set right, because the code is creating perf_month_month5 and status_month5 when in fact it should stop at 4, and so once again the records are getting flagged incorrectly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need a macro do loop, where the loop is over 1 row, multiple columns, and not over multiple rows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; CalcRates(begdate, enddate);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; start=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%sysfunc&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(inputn(&amp;amp;begdate,anydtdte9.));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; end=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%sysfunc&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(inputn(&amp;amp;enddate,anydtdte9.));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;/*determines the number of months between the two dates*/&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; dif=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%sysfunc&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(intck(month,&amp;amp;start,&amp;amp;end));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%put&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; dif=&amp;amp;dif.;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Pmt_test;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;set Pmtraw&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;cumePmt=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; i=1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%do&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%while&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(&amp;amp;i&amp;lt;&amp;amp;dif);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;if Pmt_month&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;i.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; then Pmt_month&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;i.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;cumePmt = cumePmt + Pmt_month&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;i.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;if perf_month = &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;end.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; then do; &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="1"&gt;/* terminal condition 1*/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;flag = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="1"&gt;'Last cohort of ref data'&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; i=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%eval&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(&amp;amp;dif+1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;else if total &amp;lt;= &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; then do; &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="1"&gt;/* terminal condition 2*/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;flag = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="1"&gt;'Bal_at_obs eq 0'&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; i=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%eval&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(&amp;amp;dif+1); &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;else do;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;if strip(Status_month&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;i.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;) in (&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="1"&gt;'Closed'&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;) &lt;FONT color="#008000" face="Courier New" size="1"&gt;/* terminal condition 3*/&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then do;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;flag = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="1"&gt;'Closed in current month'&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; i=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%eval&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(&amp;amp;dif+1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;else if perf_month_month&amp;amp;i = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; then do; &lt;FONT color="#008000" face="Courier New" size="1"&gt;/* terminal condition 4*/&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;flag = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="1"&gt;'End of reference data'&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; i=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%eval&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(&amp;amp;dif+1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;else if cumePmt &amp;gt;= total then do; &lt;FONT color="#008000" face="Courier New" size="1"&gt;/* terminal condition 5*/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;flag = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="1"&gt;'Cumt Pmt &amp;gt; Bal'&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; i=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%eval&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;(&amp;amp;dif+1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%end&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; CalcRates;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;%&lt;STRONG&gt;&lt;I&gt;CalcRates&lt;/I&gt;&lt;/STRONG&gt;(&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;31&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;JAN2014, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;31&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;MAY2014);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Mar 2018 23:05:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-cumulative-sum-and-exit-do-loop-when-condition/m-p/444972#M111455</guid>
      <dc:creator>PS185</dc:creator>
      <dc:date>2018-03-12T23:05:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate cumulative sum and exit do loop when condition is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-cumulative-sum-and-exit-do-loop-when-condition/m-p/444985#M111461</link>
      <description>&lt;P&gt;It would help if you provide two example datasets: one representing what you have and one representing what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I doubt if you need a macro unless you are going to repeat the task using different dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you really want to reinitialize the sum if you come across a missing value?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Mon, 12 Mar 2018 23:41:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-cumulative-sum-and-exit-do-loop-when-condition/m-p/444985#M111461</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-03-12T23:41:31Z</dc:date>
    </item>
  </channel>
</rss>

