<?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: Do loop counter in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-counter/m-p/797416#M287511</link>
    <description>&lt;P&gt;From your description, it looks like you want to calculate a rolling window of 35 years.&lt;/P&gt;
&lt;P&gt;I would do this by filling an array of 35 elements, the index calculated with mod(_n_,35). At any given timepoint, the array holds the last 35 years.&lt;/P&gt;</description>
    <pubDate>Sat, 19 Feb 2022 20:35:40 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-02-19T20:35:40Z</dc:date>
    <item>
      <title>Do loop counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-counter/m-p/797406#M287504</link>
      <description>&lt;P&gt;I have 94 years of data; I want to cumulate the data in groups of 35 years, and then calculate my metrics for years 2-36, then 3-37, etc.&amp;nbsp; I don’t know how to write macros, so I was looking at a do loop with n=1 to 35, but I don’t know how to&amp;nbsp; make it reset the variable back to the original values and work through the calculations again.&amp;nbsp; For instance, I have a withdrawal amount of $73,600.&amp;nbsp; I want it adjusted for inflation during each of the 35 years, but at the conclusion of the 35 years, I want the calculations done, and then return the withdrawal to $73,600 to start the calculations for years 2-36.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here’s what I was trying:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data merg2;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; by year;&lt;/P&gt;&lt;P&gt;do n = 1 to 35;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Retain avg1 inflate portwith portwithtot savings dcatotbal DCAnewsav dcatotval1 saving totbal ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; N+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; If N eq 1 then savings=1840000 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;portwith=73600;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if avg1&amp;gt;0&lt;/P&gt;&lt;P&gt;then inflate = portwith*(1+(avg1/100));&lt;/P&gt;&lt;P&gt;else inflate = portwith;&lt;/P&gt;&lt;P&gt;portwithtot + inflate - portwith;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*monthly withdrawals;&lt;/P&gt;&lt;P&gt;bjan= savings*(lgwt*jan+govwt*gjan)-((portwith+portwithtot)/time);&lt;/P&gt;&lt;P&gt;bfeb= savings*(lgwt*feb+govwt*gfeb)-((portwith+portwithtot)/time);&lt;/P&gt;&lt;P&gt;bmar= savings*(lgwt*mar+govwt*gmar)-((portwith+portwithtot)/time);&lt;/P&gt;&lt;P&gt;bapr= savings*(lgwt*apr+govwt*gapr)-((portwith+portwithtot)/time);&lt;/P&gt;&lt;P&gt;bmay= savings*(lgwt*may+govwt*gmay)-((portwith+portwithtot)/time);&lt;/P&gt;&lt;P&gt;bjun= savings*(lgwt*jun+govwt*gjun)-((portwith+portwithtot)/time);&lt;/P&gt;&lt;P&gt;bjul= savings*(lgwt*jul+govwt*gjul)-((portwith+portwithtot)/time);&lt;/P&gt;&lt;P&gt;baug= savings*(lgwt*aug+govwt*gaug)-((portwith+portwithtot)/time);&lt;/P&gt;&lt;P&gt;bsep= savings*(lgwt*sep+govwt*gsep)-((portwith+portwithtot)/time);&lt;/P&gt;&lt;P&gt;boct= savings*(lgwt*oct+govwt*goct)-((portwith+portwithtot)/time);&lt;/P&gt;&lt;P&gt;bnov= savings*(lgwt*nov+govwt*gnov)-((portwith+portwithtot)/time);&lt;/P&gt;&lt;P&gt;bdec= savings*(lgwt*dec+govwt*gdec)-((portwith+portwithtot)/time);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dcaBalance = bjan + bfeb + bmar + bapr + bmay + bjun + bjul + baug + bsep + boct + bnov + bdec;&lt;/P&gt;&lt;P&gt;dcatotbal + dcaBalance;&lt;/P&gt;&lt;P&gt;*savings +dcatotbal;&lt;/P&gt;&lt;P&gt;*DCAnewsav = dcatotbal + savings;&lt;/P&gt;&lt;P&gt;savings + dcabalance;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is wrong with my setup?&amp;nbsp; Thank you.&lt;/P&gt;</description>
      <pubDate>Sat, 19 Feb 2022 19:02:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-counter/m-p/797406#M287504</guid>
      <dc:creator>6071</dc:creator>
      <dc:date>2022-02-19T19:02:41Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-counter/m-p/797407#M287505</link>
      <description>&lt;P&gt;I have no idea what you are trying to do, but to answer your question you can either store the original value or re-read that observation from the dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Store it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;initial=withdrawal;
do year=1 to 35;
   ... some calculations that change withdrawal ...;
end;
withdrawal = initial;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Re-read it;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
   current=_n_;
   ... do some calculations that might change withdrawal ...
   set have(keep=withdrawal) point=current;
  ....
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 19 Feb 2022 19:29:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-counter/m-p/797407#M287505</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-19T19:29:15Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-counter/m-p/797416#M287511</link>
      <description>&lt;P&gt;From your description, it looks like you want to calculate a rolling window of 35 years.&lt;/P&gt;
&lt;P&gt;I would do this by filling an array of 35 elements, the index calculated with mod(_n_,35). At any given timepoint, the array holds the last 35 years.&lt;/P&gt;</description>
      <pubDate>Sat, 19 Feb 2022 20:35:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-counter/m-p/797416#M287511</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-19T20:35:40Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-counter/m-p/797417#M287512</link>
      <description>&lt;P&gt;PROC EXPAND does running sums (I suppose that's what you mean by "cumulate") or averages. Example:&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/etsug/etsug_expand_examples04.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmmvacdc/9.4/etsug/etsug_expand_examples04.htm&lt;/A&gt; You want to use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;convert x = x_movsum / transformout=(movsum 35);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 19 Feb 2022 20:47:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-counter/m-p/797417#M287512</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-02-19T20:47:23Z</dc:date>
    </item>
  </channel>
</rss>

