<?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 why there is difference in number of observations in both do loops ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/why-there-is-difference-in-number-of-observations-in-both-do/m-p/928035#M365161</link>
    <description>&lt;P&gt;Sorry for the basic question but I am trying to figure out why data A has only 2 obs while data B has 4.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expecting A to have 4 obs. what is the logic for having only 2 obs ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*&amp;nbsp; A */
data a;
	do i=1 to 4;
		i+1;
		output;
	end;
run;


/* B */
%macro bb;

data b;
		%do i=1 %to 4;
			i+1;
			output;
		%end;
run;

%mend bb;

%bb;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 12 May 2024 14:19:36 GMT</pubDate>
    <dc:creator>Peter0123</dc:creator>
    <dc:date>2024-05-12T14:19:36Z</dc:date>
    <item>
      <title>why there is difference in number of observations in both do loops ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-there-is-difference-in-number-of-observations-in-both-do/m-p/928035#M365161</link>
      <description>&lt;P&gt;Sorry for the basic question but I am trying to figure out why data A has only 2 obs while data B has 4.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expecting A to have 4 obs. what is the logic for having only 2 obs ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*&amp;nbsp; A */
data a;
	do i=1 to 4;
		i+1;
		output;
	end;
run;


/* B */
%macro bb;

data b;
		%do i=1 %to 4;
			i+1;
			output;
		%end;
run;

%mend bb;

%bb;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 May 2024 14:19:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-there-is-difference-in-number-of-observations-in-both-do/m-p/928035#M365161</guid>
      <dc:creator>Peter0123</dc:creator>
      <dc:date>2024-05-12T14:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: why there is difference in number of observations in both do loops ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-there-is-difference-in-number-of-observations-in-both-do/m-p/928039#M365163</link>
      <description>&lt;P&gt;%DO and DO do not do the same things!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%DO cannot access the values in a DATA step variable such as data step variable I in your code, nor can it loop through the values of a DATA step variable. %DO works on the values of macro variables only.&lt;/P&gt;</description>
      <pubDate>Sun, 12 May 2024 14:51:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-there-is-difference-in-number-of-observations-in-both-do/m-p/928039#M365163</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-05-12T14:51:29Z</dc:date>
    </item>
    <item>
      <title>Re: why there is difference in number of observations in both do loops ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-there-is-difference-in-number-of-observations-in-both-do/m-p/928044#M365165</link>
      <description>&lt;P&gt;Adding, while your data step is not wrong syntactically, as there are no ERRORs in the log, I wonder if it is doing what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
	do i=1 to 4;
		i+1;
		output;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;produces only 2 output records, with I having the values 2 and 4. If you want four records output, with values 1, 2, 3 and 4, then your line I+1 is incorrect here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
	do i=1 to 4;
		output;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 May 2024 14:55:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-there-is-difference-in-number-of-observations-in-both-do/m-p/928044#M365165</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-05-12T14:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: why there is difference in number of observations in both do loops ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-there-is-difference-in-number-of-observations-in-both-do/m-p/928045#M365166</link>
      <description>&lt;P&gt;In the first one you are incrementing the loop counter in the middle of the loop.&lt;/P&gt;
&lt;P&gt;In the second one you are not.&amp;nbsp; In fact their are NO macro statements inside the %DO loop.&amp;nbsp; Only the SAS statements you are using the macro code to generate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the first one you ran this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to 4;
  i+1;
  output;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the second one you ran this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;i+1;
output;
i+1;
output;
i+1;
output;
i+1;
output;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To have them produce the same number of observations remove the sum statement from the first one so the loop will run 4 times.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you include a SET statement into the data step so that it iterates more than once they won't produce the same results since the first one will generate I values like: 1,2,3,4,1,2,3,4,... and the second one will generate I values like 1,2,3,4,5,6,7,8,...&amp;nbsp; In that case the equivalent steps would be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
  set old;
  do i=1 to 4;
    output;
  end;
run;
%macro test;
data new;
  set old;
%do i=1 %to 4;
  i=&amp;amp;i;
  output;
%end;
run;
%mend test;
%test;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 May 2024 15:13:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-there-is-difference-in-number-of-observations-in-both-do/m-p/928045#M365166</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-12T15:13:22Z</dc:date>
    </item>
    <item>
      <title>Re: why there is difference in number of observations in both do loops ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-there-is-difference-in-number-of-observations-in-both-do/m-p/928048#M365167</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/435751"&gt;@Peter0123&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the first DATA step, variable &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; is set to 1 by the DO statement, then incremented by 1 by the sum statement (&lt;FONT face="courier new,courier"&gt;i+1;&lt;/FONT&gt;). The resulting value 2 is written to the output dataset A by the OUTPUT statement. Then the DO loop iterates, increasing the value of &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; to 3, which in turn is incremented to 4 before the next execution of the OUTPUT statement. After the 4 has been written to A (as the second observation) the DO loop increments &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; to 5, beyond the specified end value 4, which terminates the loop and then the DATA step (as there is no subsequent statement between &lt;FONT face="courier new,courier"&gt;end;&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The DATA step created by macro bb,&lt;/P&gt;
&lt;PRE&gt;data b;
i+1;
output;
i+1;
output;
i+1;
output;
i+1;
output;
run;&lt;/PRE&gt;
&lt;P&gt;initializes variable &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; to 0 (as a side effect of the sum statement) and then increments and writes the incremented value to output dataset B four times, thus creating four observations with the values 1, 2, 3, 4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the "&lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt;" in "&lt;FONT face="courier new,courier"&gt;%do i=1 %to 4;&lt;/FONT&gt;" is a &lt;EM&gt;macro variable&lt;/EM&gt;&amp;nbsp;and has nothing to do with the DATA step variable &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt;, which just happens to have the same name.&lt;/P&gt;</description>
      <pubDate>Sun, 12 May 2024 15:14:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-there-is-difference-in-number-of-observations-in-both-do/m-p/928048#M365167</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-05-12T15:14:08Z</dc:date>
    </item>
  </channel>
</rss>

