<?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: set statement in a do loop vs set statement not in do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451766#M113954</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/187797"&gt;@danielhanbitlee&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I see. Thank you for this as this is a good exercise. My explanation is that the do loop will go from year = 1 to year = 3 by 1s. In addition to this, there's the implicit loop of the data step. This will read the input dataset one observation at a time until the end of the dataset. Since there are four observations, the output dataset will also have four observations even though the do loop only goes from 1 to 3. So, even though the set statement is inside a do loop, the set statement will be controlled by the implicit data step loop. That is, all the observations in the set statement will only be read once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please correct/clarify my understanding as you see fit.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Most SAS data step do not stop at the end. Instead they stop when you read past the end of the input. So in your situation the data step is trying to read three observations per iteration. So the step iterates only two times. The first time it completes the DO loop, but on the second it stops the second time around the DO loop when it tries to read past the end of the input dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This means that a normal data step with just a single SET or INPUT statement will iterate N+1 times.&amp;nbsp; &amp;nbsp;The last iteration will stop when it reads past the input and never make it to the end of the data step (where the implied OUTPUT statement runs).&lt;/P&gt;</description>
    <pubDate>Fri, 06 Apr 2018 05:17:54 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-04-06T05:17:54Z</dc:date>
    <item>
      <title>set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451345#M113758</link>
      <description>&lt;P&gt;I have the following dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data banks;
	input name : $ 13. rate;
	datalines;
FirstCapital 0.0718
DirectBank 0.0721
VirtualDirect 0.0728
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am comparing between two blocks of codes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First code block:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data newbank (drop = year);
	do year=1 to 3;
		set banks;
		output;
	end;
run;
proc print data = newbank;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output to first code block:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Obs	name	rate
1	FirstCapital	0.0718
2	DirectBank	0.0721
3	VirtualDirect	0.0728&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;Second code block:&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data newbank;
	set banks;
		output;
	set banks;
		output;
	set banks;
		output;
run;
proc print data = newbank;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output to second code block:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Obs	name	rate
1	FirstCapital	0.0718
2	FirstCapital	0.0718
3	FirstCapital	0.0718
4	DirectBank	0.0721
5	DirectBank	0.0721
6	DirectBank	0.0721
7	VirtualDirect	0.0728
8	VirtualDirect	0.0728
9	VirtualDirect	0.0728&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Question: Why doesn't the first code block have the same output as the second code block? Why is there a difference if I use a set statement in a do loop vs using a set statement not in a do loop?&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 23:06:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451345#M113758</guid>
      <dc:creator>danielhanbitlee</dc:creator>
      <dc:date>2018-04-04T23:06:28Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451351#M113764</link>
      <description>&lt;P&gt;What's happening in the second code block is that you effectively have three versions of the bank data set open so it will write the first observation three times, once for each version of the data set and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You sometimes see the same data set open more than once in a data step but it is a usually in the context of a DOW loop&amp;nbsp;&lt;A href="http://support.sas.com/resources/papers/proceedings12/052-2012.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings12/052-2012.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;&lt;EM&gt;(editor's note: removed trailing blank from link)&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 06:50:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451351#M113764</guid>
      <dc:creator>ChrisBrooks</dc:creator>
      <dc:date>2018-04-05T06:50:50Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451353#M113766</link>
      <description>&lt;P&gt;I see. What about in the first code block? Don't I have the data set open three times as well since the set statement is inside a DO loop?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 00:03:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451353#M113766</guid>
      <dc:creator>danielhanbitlee</dc:creator>
      <dc:date>2018-04-05T00:03:22Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451354#M113767</link>
      <description>&lt;P&gt;Also, can you resend the&amp;nbsp;pdf link? It doesn't seem to work for me.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 00:04:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451354#M113767</guid>
      <dc:creator>danielhanbitlee</dc:creator>
      <dc:date>2018-04-05T00:04:58Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451356#M113768</link>
      <description>&lt;P&gt;No because in the first code block you only have one SET statement - the first time it executes it opens the data set and reads the first observation, the second time it reads the second observation etc. In the second code block you have three SET statements, each of which opens the data set and reads the first statement and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll try the link again&amp;nbsp;&lt;A href="http://support.sas.com/resources/papers/proceedings12/052-2012.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings12/052-2012.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 00:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451356#M113768</guid>
      <dc:creator>ChrisBrooks</dc:creator>
      <dc:date>2018-04-05T00:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451361#M113769</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; In Programming 1 (a free e-learning class) we discuss the concept of the Program Data Vector (PDV) and how SAS loads the input dataset information into the PDV and how information is written OUT of the PDV to the new dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Take a look at these alternate scenarios. I have changed your orginal code, so there are numbered output files, so that YEAR is kept and a new variable named WHATSET tells you what SET statement in the program supplied the variable information for that row. When you understand the PDV, you will understand why my #1 and my #2 are the same and only have 3 observations in the output dataset. And, when you understand the PDV, you will understand why the variable WHATSET is 'set3' in #6, but is 'set1' in #1 and #2.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="set_do_loop.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/19631iB05AF98BFED203E1/image-size/large?v=v2&amp;amp;px=999" role="button" title="set_do_loop.png" alt="set_do_loop.png" /&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; #3 and #4 produce identical output -- 9 observations -- one program with a DO loop and one program without a DO loop. #5 also produces 9 observations, but with slightly different values for YEAR and WHATSET.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Here's the code that produces all the outputs, including the 2 shown above.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data banks;
	input name : $ 13. rate;
	datalines;
FirstCapital 0.0718
DirectBank 0.0721
VirtualDirect 0.0728
;
run;

data newbank1;
	do year=1 to 3;
		set banks;
	    whatset='set1';
		output;
	end;
run;
proc print data = newbank1;
title 'NewBank1 -- SET inside DO loop';
run;

data newbank2;
    set banks;
	year = _n_;
	whatset = 'set1';
    output;
run;
proc print data = newbank2;
title 'NewBank2 -- NO DO loop at all';
run;


data newbank3;
    set banks;
	whatset = 'set1';
	year=1;
    output;
	year = 2;
	output;
	year = 3;
	output;
run;
proc print data = newbank3;
title 'NewBank3 -- NO DO loop but have 3 outputs';
run;



data newbank4;
    set banks;
	whatset = 'set1';
	do year=1 to 3;
		output;
	end;
run;
proc print data = newbank4;
title 'NewBank4 -- SET outside DO loop but OUTPUT inside DO loop';
run;


data newbank5;
     set banks ;
	    whatset = 'set1';
	    year = _n_;
		output;
	set banks ;
	    whatset = 'set2';
	    year = _n_;
		output;
	set banks;
	    whatset = 'set3';
	    year = _n_;
		output;
run;
proc print data = newbank5;
  title 'NewBank5 -- NO DO Loop 3 SET statements and 3 OUTPUTS';
run;


data newbank6;
     set banks ;
	    whatset = 'set1';
	    year = _n_;
	set banks ;
	    whatset = 'set2';
	    year = _n_;
	set banks;
	    whatset = 'set3';
	    year = _n_;
		output;
run;
proc print data = newbank6;
  title 'NewBank6 -- NO DO Loop 3 SET statements ONLY 1 OUTPUT';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; What did you expect to get from your original code with the SET inside the DO loop? Based on how SAS works, the DO Loop didn't get you much if all you wanted to do was read one record and write one record. The DATA step (as you can see by #2) is an implied looping structure. It reads every obs in the input dataset until the end of file is reached. In #2, for every obs that is read, an obs is written. You have to understand HOW to use the SET statement without a DO loop and how to use the OUTPUT statement without a DO loop before you start to put a SET statement inside a DO loop (IMO).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 00:54:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451361#M113769</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2018-04-05T00:54:31Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451414#M113792</link>
      <description>&lt;P&gt;To further give you food for thought, run this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data banks;
	input name : $ 13. rate;
	datalines;
FirstCapital 0.0718
DirectBank 0.0721
VirtualDirect 0.0728
fourthbank 0.05
;
run;

data newbank (drop = year);
	do year=1 to 3;
		set banks;
		output;
	end;
run;

proc print data = newbank;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and try to explain to us what happens.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 06:54:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451414#M113792</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-05T06:54:57Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451467#M113830</link>
      <description>&lt;P&gt;Each SET statement will assign a pointer to the table, even if they are the same table.&lt;/P&gt;
&lt;P&gt;Therefore,the first one has only one SET/pointer , will output three obs.&lt;/P&gt;
&lt;P&gt;the second one have three SET/pointer , will output 3*3 obs .&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 11:44:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451467#M113830</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-04-05T11:44:27Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451665#M113912</link>
      <description>&lt;P&gt;Ah got it. That was helpful. Thank you again!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would accept this as a solution as well but I'm not sure how to have multiple answers as solutions.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 21:28:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451665#M113912</guid>
      <dc:creator>danielhanbitlee</dc:creator>
      <dc:date>2018-04-05T21:28:44Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451693#M113923</link>
      <description>&lt;P&gt;Thank you for the thorough response. It is very helpful. From all your examples, I am beginning to understand that there are two different loops going on in #1. That is, first, there's one loop for the do loop (year = 1 to year = 3). And then, second, there's the&amp;nbsp;implicit loop of the data step. This reads all the observations in the input dataset (from the set statement) independent of the do loop. Please correct/clarify if my understanding is wrong. Otherwise, this has been very helpful. Thanks again.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 21:28:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451693#M113923</guid>
      <dc:creator>danielhanbitlee</dc:creator>
      <dc:date>2018-04-05T21:28:06Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451695#M113924</link>
      <description>&lt;P&gt;Thank you for the reply. This is very helpful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would accept this as a solution as well but I'm not sure how to have multiple answers as solutions.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 21:26:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451695#M113924</guid>
      <dc:creator>danielhanbitlee</dc:creator>
      <dc:date>2018-04-05T21:26:55Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451697#M113925</link>
      <description>&lt;P&gt;I see. Thank you for this as this is a good exercise. My explanation is that the do loop will go from year = 1 to year = 3 by 1s. In addition to this, there's the implicit loop of the data step. This will read the input dataset one observation at a time until the end of the dataset. Since there are four observations, the output dataset will also have four observations even though the do loop only goes from 1 to 3. So, even though the set statement is inside a do loop, the set statement will be controlled by the implicit data step loop. That is, all the observations in the set statement will only be read once.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please correct/clarify my understanding as you see fit.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 21:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451697#M113925</guid>
      <dc:creator>danielhanbitlee</dc:creator>
      <dc:date>2018-04-05T21:33:58Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451707#M113927</link>
      <description>&lt;P&gt;Another potentially useful exercise: Compare the two resulting sets from:&lt;/P&gt;
&lt;PRE&gt;data newbank;
	set banks;
		output;
	set banks;
		output;
	set banks;
		output;
run;

data newbank2;
	set banks
	    banks
	    banks
	;
run;

&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Apr 2018 21:55:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451707#M113927</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-05T21:55:07Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451715#M113933</link>
      <description>&lt;P&gt;Thank you for the exercise. This is a good point and it helps me to understand what is going on behind the scenes when combining datasets.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 23:39:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451715#M113933</guid>
      <dc:creator>danielhanbitlee</dc:creator>
      <dc:date>2018-04-05T23:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451766#M113954</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/187797"&gt;@danielhanbitlee&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I see. Thank you for this as this is a good exercise. My explanation is that the do loop will go from year = 1 to year = 3 by 1s. In addition to this, there's the implicit loop of the data step. This will read the input dataset one observation at a time until the end of the dataset. Since there are four observations, the output dataset will also have four observations even though the do loop only goes from 1 to 3. So, even though the set statement is inside a do loop, the set statement will be controlled by the implicit data step loop. That is, all the observations in the set statement will only be read once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please correct/clarify my understanding as you see fit.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Most SAS data step do not stop at the end. Instead they stop when you read past the end of the input. So in your situation the data step is trying to read three observations per iteration. So the step iterates only two times. The first time it completes the DO loop, but on the second it stops the second time around the DO loop when it tries to read past the end of the input dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This means that a normal data step with just a single SET or INPUT statement will iterate N+1 times.&amp;nbsp; &amp;nbsp;The last iteration will stop when it reads past the input and never make it to the end of the data step (where the implied OUTPUT statement runs).&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 05:17:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451766#M113954</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-04-06T05:17:54Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451774#M113958</link>
      <description>&lt;P&gt;Almost completely correct. The data step has an implicit "on end-of-file goto end", so as soon as the set statement tries to read &lt;EM&gt;past&lt;/EM&gt; eof, it exits the loop without the error you'd get in another programming language.&lt;/P&gt;
&lt;P&gt;The set is primarily controlled by the do loop, but it also has this "safety valve" that terminates the data step when the do loop makes its second iteration in the second iteration of the data step, where it tries to read a fifth observation that isn't there.&lt;/P&gt;
&lt;P&gt;See this example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
put "_n_=" _n_;
put "eof before: " eof;
set sashelp.class end=eof;
put "eof after: " eof;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can see that the data step does 20 iterations, although sashelp.class only has 19 observations. And eof is set when the last observation is read, carries over, and is acted upon as soon as set tries to read past eof.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 06:12:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451774#M113958</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-06T06:12:16Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451782#M113962</link>
      <description>&lt;P&gt;Thank you for the response. I think I'm beginning to understand better. So, just to make sure, let me try to explain.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will use the code that you gave previously, except I&amp;nbsp;won't drop the variable "year" from the do loop:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data banks;
	input name : $ 13. rate;
	datalines;
FirstCapital 0.0718
DirectBank 0.0721
VirtualDirect 0.0728
fourthbank 0.05
;
run;

data newbank;
	do year=1 to 3;
		set banks;
		output;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Output:&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;PRE&gt;&lt;CODE class=" language-sas"&gt;Obs	year	name	rate
1	1	FirstCapital	0.0718
2	2	DirectBank	0.0721
3	3	VirtualDirect	0.0728
4	1	fourthbank	0.0500&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here's my explanation of what is going on (also I have a question in &lt;STRONG&gt;&lt;FONT color="#FF6600"&gt;red font color&lt;/FONT&gt;&lt;/STRONG&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&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;1. First iteration of data step&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;First iteration of do loop (year = 1)&lt;OL&gt;&lt;LI&gt;First observation of the dataset is read and output is given (code: set banks; output;)&lt;/LI&gt;&lt;/OL&gt;&lt;/LI&gt;&lt;LI&gt;Second iteration of do loop (year = 2)&lt;OL&gt;&lt;LI&gt;Second observation of the dataset is read and output is given (code: set banks; output;)&lt;/LI&gt;&lt;/OL&gt;&lt;/LI&gt;&lt;LI&gt;Third iteration of do loop (year = 3)&lt;OL&gt;&lt;LI&gt;Third observation of the dataset is read and output is given (code: set banks; output;)&lt;/LI&gt;&lt;/OL&gt;&lt;/LI&gt;&lt;LI&gt;End of do loop and end of first iteration of data step&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;2. Second iteration of data step&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;First iteration of do loop (year = 1)&lt;OL&gt;&lt;LI&gt;Fourth observation of the dataset is read and output is given (code: set banks; output;)&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;&lt;FONT color="#FF6600"&gt;How does SAS know to read the fourth observation of the input dataset here? Is there some sort of a pointer?&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;LI&gt;End of file reached. EOF = 1&lt;/LI&gt;&lt;/OL&gt;&lt;/LI&gt;&lt;LI&gt;Second iteration of do loop (year = 2)&lt;OL&gt;&lt;LI&gt;Terminate data step because EOF&amp;nbsp; = 1. No output is given&lt;/LI&gt;&lt;/OL&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Please correct/clarify.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 07:10:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451782#M113962</guid>
      <dc:creator>danielhanbitlee</dc:creator>
      <dc:date>2018-04-06T07:10:03Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451785#M113964</link>
      <description>&lt;P&gt;You are absolutely correct.&lt;/P&gt;
&lt;P&gt;Regarding your question in red:&lt;/P&gt;
&lt;P&gt;Each set (or merge) statement keeps its own pointer(s) throughout execution and carries them over from one data step iteration to the next. These pointers can be manipulated (see key= and point= options for the set statement), which can be the base for some tricky programming (but caution needs to be exercised).&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 07:18:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/451785#M113964</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-06T07:18:34Z</dc:date>
    </item>
    <item>
      <title>Re: set statement in a do loop vs set statement not in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/452020#M114041</link>
      <description>&lt;P&gt;Got it. Makes more sense now. Thank you so much!&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 18:01:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/set-statement-in-a-do-loop-vs-set-statement-not-in-do-loop/m-p/452020#M114041</guid>
      <dc:creator>danielhanbitlee</dc:creator>
      <dc:date>2018-04-06T18:01:38Z</dc:date>
    </item>
  </channel>
</rss>

