<?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: Arrays and do loops in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Arrays-and-do-loops/m-p/518315#M140272</link>
    <description>&lt;P&gt;A useful exercise to see the the development of i is to put an output statement inside the do loop and force an output statement at the bottom of the data step (faking the implicit output statement) like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp2;
set temp1;
array z[2] x y;
do i = 1 to 2;
	if z[i]=. then z[i]=0;
   output;
end;
output;  /* Implicit output statement */
run;

proc print data=temp2;
title 'temp2';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 04 Dec 2018 07:22:50 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2018-12-04T07:22:50Z</dc:date>
    <item>
      <title>Arrays and do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-and-do-loops/m-p/518313#M140270</link>
      <description>&lt;P&gt;Can anyone tell me what the variable i does in this do loop? Does it count the times it's ran horizontally rather than vertically and line feeds after it gets to i? I have attached the output as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/***************************************/

data temp1;
input x y;
*if x=. then x=0; 
datalines;
3 .
. 4
. .
2 6 
;
run;
/***************************************/

proc print data=temp1;
title 'temp1';
run;
/***************************************/

data temp2;
set temp1;
array z[2] x y;
do i = 1 to 2;
	if z[i]=. then z[i]=0;
end;
/***************************************/

proc print data=temp2;
title 'temp2';
run;
/***************************************/
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is my output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Obs&amp;nbsp;&amp;nbsp; x&amp;nbsp;&amp;nbsp;&amp;nbsp; y&amp;nbsp;&amp;nbsp;&amp;nbsp; i&lt;BR /&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;BR /&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;BR /&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;BR /&gt;4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In other words why does i = 3 for every iteration of the do loop in this array?&lt;/P&gt;</description>
      <pubDate>Tue, 04 Dec 2018 07:05:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-and-do-loops/m-p/518313#M140270</guid>
      <dc:creator>clancaster</dc:creator>
      <dc:date>2018-12-04T07:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays and do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-and-do-loops/m-p/518314#M140271</link>
      <description>&lt;P&gt;&lt;STRONG&gt;i&lt;/STRONG&gt; is used in your do loop as an iterating variable. You are essentially creating the line&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if z[i]=. then z[i]=0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;twice with the values &lt;STRONG&gt;i=1 and i=2&lt;/STRONG&gt;.&amp;nbsp;Here z[1] means the first entry of the array &lt;STRONG&gt;z&amp;nbsp;&lt;/STRONG&gt;and z[2] is the second entry of the array.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To answer your question:&amp;nbsp;&lt;STRONG&gt;&lt;SPAN&gt;why does i = 3 for every iteration of the do loop in this array?&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It does not equal 3 for every iteration of the do loop. When the&amp;nbsp;line above is executed for &lt;STRONG&gt;i=2&lt;/STRONG&gt;, the do loop increases i to 3 and checks whether 3 is in the do loop bounds. It is not, so the data step jumps to the line after the do loop. That is why &lt;STRONG&gt;i=3&lt;/STRONG&gt;&amp;nbsp;appears in every outputted record in temp2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Dec 2018 07:18:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-and-do-loops/m-p/518314#M140271</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-12-04T07:18:45Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays and do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-and-do-loops/m-p/518315#M140272</link>
      <description>&lt;P&gt;A useful exercise to see the the development of i is to put an output statement inside the do loop and force an output statement at the bottom of the data step (faking the implicit output statement) like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp2;
set temp1;
array z[2] x y;
do i = 1 to 2;
	if z[i]=. then z[i]=0;
   output;
end;
output;  /* Implicit output statement */
run;

proc print data=temp2;
title 'temp2';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Dec 2018 07:22:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-and-do-loops/m-p/518315#M140272</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-12-04T07:22:50Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays and do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-and-do-loops/m-p/518319#M140274</link>
      <description>&lt;P&gt;You are mixing data step iterations and do loop iterations in your mind.&lt;/P&gt;
&lt;P&gt;Do loop iterations are controlled by the corresponding do and end statements, while data step iterations go from the data statement to the next step boundary (usually the run statement) and are controlled by the input into the data step (in your case, the number of observations in the dataset named in the set statement).&lt;/P&gt;
&lt;P&gt;The do loop runs its course for &lt;EM&gt;every&lt;/EM&gt; data step iteration, and since it ends when the iteration variable goes &lt;EM&gt;past&lt;/EM&gt; the end value of the do statement, you get the 3 every time the do loop finishes. The implicit output at the end of the data step iteration then writes that value to the new dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suggest you take the time and work through the documentation for how a data step works, which can be found &lt;A href="http://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lrcon&amp;amp;docsetTarget=p1topuaeb1ikf0n11f6ibw5ftral.htm&amp;amp;locale=en" target="_blank"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hint: a very nice tool to see what's going on is the put statement, which allows you to write values to the log. Putting one inside the do loop and one after will nicely show the development of values.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Dec 2018 07:47:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-and-do-loops/m-p/518319#M140274</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-12-04T07:47:50Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays and do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-and-do-loops/m-p/518389#M140292</link>
      <description>&lt;P&gt;You will learn the most, the fastest, if you construct your own program to provide some insight.&amp;nbsp; For this case, you could try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp2;
   set temp1;
   array z{2} x y;
   do i = 1 to 2;
      if z{i} . then z{i} = 0;
      put 'Inside the loop:  ' i=;
   end;
   put 'Outside the loop:  ' i=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Dec 2018 14:09:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-and-do-loops/m-p/518389#M140292</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-12-04T14:09:51Z</dc:date>
    </item>
  </channel>
</rss>

