<?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 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252451#M47913</link>
    <description>&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings09/038-2009.pdf" target="_self"&gt;DOW&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 25 Feb 2016 17:56:51 GMT</pubDate>
    <dc:creator>Haikuo</dc:creator>
    <dc:date>2016-02-25T17:56:51Z</dc:date>
    <item>
      <title>Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252447#M47910</link>
      <description>&lt;P&gt;&lt;STRONG&gt;How does the below code work?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;QUESTION &amp;nbsp;&lt;BR /&gt;The SAS data set BANKS is listed below:&lt;BR /&gt;BANKS&lt;BR /&gt;name rate&lt;BR /&gt;FirstCapital0.0718&lt;BR /&gt;DirectBank0.0721&lt;BR /&gt;VirtualDirect0.0728&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The following SAS program is submitted:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data newbank;&lt;BR /&gt;do year = 1 to 3;&lt;BR /&gt;set banks;&lt;BR /&gt;capital + 5000;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;Which one of the following represents how many observations and variables will&lt;BR /&gt;exist in the SAS data set NEWBANK?&lt;BR /&gt;&lt;BR /&gt;A. 0 observations and 0 variables&lt;BR /&gt;B. 1 observations and 4 variables&lt;BR /&gt;C. 3 observations and 3 variables&lt;BR /&gt;D. 9 observations and 2 variables&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ans –B&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 17:51:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252447#M47910</guid>
      <dc:creator>Bulleride</dc:creator>
      <dc:date>2016-02-25T17:51:51Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252451#M47913</link>
      <description>&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings09/038-2009.pdf" target="_self"&gt;DOW&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 17:56:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252451#M47913</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-02-25T17:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252453#M47915</link>
      <description>Sorry! I didn't get you.</description>
      <pubDate>Thu, 25 Feb 2016 17:59:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252453#M47915</guid>
      <dc:creator>Bulleride</dc:creator>
      <dc:date>2016-02-25T17:59:46Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252458#M47918</link>
      <description>&lt;P&gt;The data step you presented involved a coding structure called 'DOW', which has a rich history and broad implementation , and it is hard to explain it in a short&amp;nbsp;post like this without causing more misunderstanding.You will need to read some paper to really under the in and out of it.&amp;nbsp; Yes, the correct is B.&amp;nbsp; In a very short (not much information) version of explanation, the code presented creates two new variables and only output once at the end of the do-loop. Please read the paper to figure out why.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 18:09:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252458#M47918</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-02-25T18:09:51Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252461#M47921</link>
      <description>Oh! Thanks a lot &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Thu, 25 Feb 2016 18:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252461#M47921</guid>
      <dc:creator>Bulleride</dc:creator>
      <dc:date>2016-02-25T18:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252463#M47923</link>
      <description>&lt;P&gt;This little piece of the SAS code, regardless its&amp;nbsp;insignificant size, &amp;nbsp;touches several most&amp;nbsp;fundamental concepts of data step, and Paul explains much better than me.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 18:15:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252463#M47923</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-02-25T18:15:06Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252484#M47938</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/74980"&gt;@Bulleride﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The paper linked by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4877"&gt;@Haikuo﻿&lt;/a&gt;&amp;nbsp;is great, but it's fairly&amp;nbsp;advanced, I think. Here is a more elementary one:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www2.sas.com/proceedings/sugi28/099-28.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi28/099-28.pdf&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 18:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252484#M47938</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-02-25T18:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252496#M47948</link>
      <description>Thanks @FreelanceReinhard &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 25 Feb 2016 19:17:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/252496#M47948</guid>
      <dc:creator>Bulleride</dc:creator>
      <dc:date>2016-02-25T19:17:09Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312821#M67875</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Thanks for a wonderful&amp;nbsp;document (&lt;A href="http://www2.sas.com/proceedings/sugi28/099-28.pdf" target="_blank" rel="nofollow noopener noreferrer"&gt;http://www2.sas.com/proceedings/sugi28/099-28.pdf&lt;/A&gt;&lt;SPAN&gt;.) first of all. Although, i have not been&amp;nbsp;able to exactly decipher what's happening in the question above.Because in the pdf, the do loop is controlled by "last.pt" so all the data for a particular pt is iterated within the loop and then when the control reaches back to data step we have another patient. In this particular example however,&lt;BR /&gt;&lt;BR /&gt;The SAS data set BANKS is listed below:&lt;BR /&gt;&lt;SPAN&gt;BANKS &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;name rate &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;FirstCapital0.0718 &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;DirectBank0.0721 &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;VirtualDirect0.0728 &lt;/SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;The following SAS program is submitted: &lt;/SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;data newbank; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;do year = 1 to 3; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;set banks; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;capital + 5000; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;end; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run; &lt;/SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;I don't think the same thing applies.It IS&amp;nbsp;a DOW,just of a different sort.Could you please help out in understanding it's iterations.&lt;BR /&gt;&lt;BR /&gt;Thanks in advance!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Nov 2016 17:42:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312821#M67875</guid>
      <dc:creator>Alisha93</dc:creator>
      <dc:date>2016-11-19T17:42:54Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312835#M67879</link>
      <description>&lt;P&gt;Hi Alisha93,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Reading the old question from Bulleride again (now, after almost 9 months), I would hesitate to call this DO loop a &lt;EM&gt;DOW&lt;/EM&gt; loop, because the typical UNTIL condition is missing here. (Of course, one could add such a condition &lt;EM&gt;pro forma&lt;/EM&gt;: &lt;FONT face="courier new,courier"&gt;until(year&amp;gt;3)&lt;/FONT&gt; should fit.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Apparently, the purpose of the code shown is just to test the reader's understanding of how the SAS data step works. It is not suitable for demonstrating the benefits of a DOW loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can see more easily how the code works if you insert diagnostic PUT statements:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data newbank; 
put 'Before loop: ' _all_;
do year = 1 to 3; 
  put 'In loop, before SET: ' _all_;
  set banks; 
  put 'In loop, after SET: ' _all_;
  capital + 5000; 
end;
put 'After loop: ' _all_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above data step will write the following lines to the log:&lt;/P&gt;
&lt;PRE&gt;Before loop: year=. name=  rate=. capital=0 _ERROR_=0 _N_=1
In loop, before SET: year=1 name=  rate=. capital=0 _ERROR_=0 _N_=1
In loop, after SET: year=1 name=FirstCapital rate=0.0718 capital=0 _ERROR_=0 _N_=1
In loop, before SET: year=2 name=FirstCapital rate=0.0718 capital=5000 _ERROR_=0 _N_=1
In loop, after SET: year=2 name=DirectBank rate=0.0721 capital=5000 _ERROR_=0 _N_=1
In loop, before SET: year=3 name=DirectBank rate=0.0721 capital=10000 _ERROR_=0 _N_=1
In loop, after SET: year=3 name=VirtualDirect rate=0.0728 capital=10000 _ERROR_=0 _N_=1
After loop: year=4 name=VirtualDirect rate=0.0728 capital=15000 _ERROR_=0 _N_=1
Before loop: year=. name=VirtualDirect rate=0.0728 capital=15000 _ERROR_=0 _N_=2
In loop, before SET: year=1 name=VirtualDirect rate=0.0728 capital=15000 _ERROR_=0 _N_=2&lt;/PRE&gt;
&lt;P&gt;Within the first iteration of the data step (see the lines with _N_=1 written by the PUT statements) the SET statement is executed three times as part of the body of the DO loop. It writes&amp;nbsp;the first, second and third observation, resp., of dataset BANKS to the program data vector (PDV), each time overwriting the previous contents of the PDV. Thus, after the loop (YEAR=4), NAME and RATE only from the last observation are present in the PDV and when the RUN statement is reached, these values, together with YEAR=4 and CAPITAL=15000, are automatically written to dataset NEWBANK. (Variable CAPITAL had been initialized to 0 and incremented by 5000 in each iteration of the DO loop by means of the Sum statement.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At the beginning of the second iteration of the data step (_N_=2) variable YEAR is reinitialized to missing, whereas the values of NAME, RATE and CAPITAL are retained. (For NAME and RATE this is because of the SET statement, for CAPITAL it's a feature of the Sum statement.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now the DO loop starts again with year=1 (as is shown by the last line written by the PUT statements). But as soon as the SET statement attempts to read the non-existing fourth observation of dataset BANKS, the data step terminates. So, NEWBANK contains one observation with four variables (year=4 name=VirtualDirect rate=0.0728 capital=15000), resulting from the first iteration of the data step (which in turn included three iterations of the DO loop).&lt;/P&gt;</description>
      <pubDate>Sat, 19 Nov 2016 23:12:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312835#M67879</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-11-19T23:12:10Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312839#M67883</link>
      <description>&lt;P&gt;A SET statement reads a new row from an input SAS table every single time it's called. You've got&amp;nbsp;&lt;SPAN&gt;&lt;EM&gt;set banks;&lt;/EM&gt;&amp;nbsp;within a do loop which iterates 3 times and though you are reading 3 lines of input rows in total.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You don't have an&amp;nbsp;&lt;EM&gt;OUTPUT&lt;/EM&gt; statement within the do loop and with SAS if there is no explicit output statement in the code then SAS executes one at the very end before the &lt;EM&gt;RUN&lt;/EM&gt;. In your case that's outside of the DO LOOP and therefore only one row gets written to the target table NEWBANK.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;As for the number of variables: &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Every single data step has a compilation and an execution phase. In the compilation phase all variables get created in the PDV (=all variables coming from the source table and all variables you create within the data step). This sums up to 4 variable in your case.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;In the execution phase if your Set statement wouldn't be in a Do Loop then SAS would read a row from the source table, execute all the code in your data step until it reaches the RUN (also executing the implicit OUTPUT so writing a row to the output table for every row coming from the input table). Because your SET statement is within a Do Loop SAS iterates only once through the data step; and that's why you're only getting a single row in the output data set (=row 3 from the input data set plus the modifications).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You can find the detailed documentation of how this works here.&amp;nbsp;It's very worthwhile to spend the time and understand this.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/68089/HTML/default/viewer.htm#p08a4x7h9mkwqvn16jg3xqwfxful.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrcon/68089/HTML/default/viewer.htm#p08a4x7h9mkwqvn16jg3xqwfxful.htm&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Nov 2016 00:14:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312839#M67883</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-11-20T00:14:33Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312867#M67891</link>
      <description>Thanks for an elaborate explaination.It cleared quite a few concepts for me,except in the second iteration where the values of Name and Rate are retained. Aren't all the non-retaining variables set to missing in the PDV ,at the top of DATA step?Could you also tell me what is the exception here.</description>
      <pubDate>Sun, 20 Nov 2016 06:23:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312867#M67891</guid>
      <dc:creator>Alisha93</dc:creator>
      <dc:date>2016-11-20T06:23:36Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312873#M67893</link>
      <description>&lt;P&gt;The reasons why some of the variables are automatically retained here can be found in the first two bullet points of section "Redundancy" in the documentation of the RETAIN statement:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/syntaxidx/68719/HTML/default/index.htm#/documentation/cdl/en/lestmtsref/68024/HTML/default/p0t2ac0tfzcgbjn112mu96hkgg9o.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/syntaxidx/68719/HTML/default/index.htm#/documentation/cdl/en/lestmtsref/68024/HTML/default/p0t2ac0tfzcgbjn112mu96hkgg9o.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Nov 2016 11:41:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312873#M67893</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-11-20T11:41:09Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312916#M67922</link>
      <description>&lt;P&gt;Fair Enough!Thanks a ton &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Nov 2016 17:19:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312916#M67922</guid>
      <dc:creator>Alisha93</dc:creator>
      <dc:date>2016-11-20T17:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312931#M67926</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/71866"&gt;@alisha﻿&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;In your code sample there is only a single iteration through the data step (because the SET statement is inside the Do Loop).&lt;/P&gt;</description>
      <pubDate>Sun, 20 Nov 2016 22:02:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/312931#M67926</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-11-20T22:02:40Z</dc:date>
    </item>
  </channel>
</rss>

