<?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: Understanding behaviour of  set statement inside do loops in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627440#M185235</link>
    <description>&lt;P&gt;Isn't curobs= enough to show the current obs pointer for a given set statement?&lt;/P&gt;</description>
    <pubDate>Wed, 26 Feb 2020 08:45:51 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-02-26T08:45:51Z</dc:date>
    <item>
      <title>Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627289#M185135</link>
      <description>&lt;P&gt;Hello, I am a beginning SAS programmer trying to wrap my head around understand the idea behind putting a set statement inside of a do loop. My general understanding of `set lib.table` is that it reads the _N_'th observation from the&amp;nbsp; given table where _N_ is the PDV's internal counter.&amp;nbsp; Inside of a do loop I was surprised to find that it looks like it has its own _N_ that starts back from 1, I.E:&lt;/P&gt;&lt;PRE&gt; if _N_ = 1 do i=10 to 20; set lib.table; end;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; will give you the first 10 observations from lib.table regardless of the index of i, or the current _N_ of the data step.&amp;nbsp; That is, the above code appears no different then&amp;nbsp; if I had started on the 10th observation for an appropriately sized data set:&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; if _N_ = 10 then do i=10 to 20; set lib.table; end;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;also, the following code threw me for a loop:&lt;/P&gt;&lt;PRE&gt;data test;
        input name : $ 13. rate;
	datalines;
A 1
B 2
C 3
;
run;

data test2;
	set test;

	do var=1 to 2;
		set test;
		output;
	end;
run;&lt;/PRE&gt;&lt;P&gt;Here the output table is&lt;/P&gt;&lt;PRE&gt;name     rate     var
 A	  1	   1
 B 	  2	   2
 C 	  3	   1&lt;/PRE&gt;&lt;P&gt;Q1&amp;nbsp;&amp;nbsp; so my understanding is the data step starts its iteration _N_= 1, gets to the do loop and writes A and B rows to data steps, exits, goes to _N_ = 2, starts the do loop and surprisingly somehow retains its internal _N2_ value to go to C row,&amp;nbsp; goes to i=2 and runs out of data so calls a quit and both the do and data step are exited out of.&amp;nbsp;&amp;nbsp; Is this correct understanding? Its kind of throwing me for a loop that the internal do statements variables are scoped to the entire data step instead of each iteration...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my other questions&lt;/P&gt;&lt;P&gt;Q2&amp;nbsp;&amp;nbsp; It kind of bothers me that set changes its meaning inside of a do statement ( the idea seems similar to subsetting if statements in that some code is changing the behavior other code without referencing it), so I'd love some reason I can give myself for why that is rational behavior, and what class of other sas commands I can expect to change behaviors inside of something else.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Q3 &amp;nbsp;&amp;nbsp; Is my idea right that everything hangs on some internal _N_ counter? using set multiple times doesnt go to the next row, so it has to be some inherent property of what is calling it that indicates what row set will access.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Q4&amp;nbsp;&amp;nbsp;&amp;nbsp; say I wanted to use set and a do loop from the 50th to 60th observation.&amp;nbsp; Is there a nice way to do that instead of some if statement while iterating through every observation?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 19:44:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627289#M185135</guid>
      <dc:creator>weg</dc:creator>
      <dc:date>2020-02-25T19:44:55Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627317#M185157</link>
      <description>&lt;P&gt;Please take note that:&lt;/P&gt;
&lt;P&gt;1. Each SET statement uses its own internal pointer.&lt;/P&gt;
&lt;P&gt;2. _N_ is a data step iteration counter.&lt;/P&gt;
&lt;P&gt;This should answer your questions and explain what you are seeing. Tell us if it doesn't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 20:29:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627317#M185157</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-02-25T20:29:29Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627321#M185159</link>
      <description>&lt;P&gt;Understanding the "Whitlock DO-loop" should also help, and who better to explain it that Paul Dorfman:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;A href="https://www.lexjansen.com/nesug/nesug08/hw/hw02.pdf" target="_blank"&gt;https://www.lexjansen.com/nesug/nesug08/hw/hw02.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 20:41:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627321#M185159</guid>
      <dc:creator>GGO</dc:creator>
      <dc:date>2020-02-25T20:41:00Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627331#M185164</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By internal pointer, I am assuming that it works the same way as _N_? and it only is thrown out when the data step completes?&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 21:09:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627331#M185164</guid>
      <dc:creator>weg</dc:creator>
      <dc:date>2020-02-25T21:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627332#M185165</link>
      <description>&lt;P&gt;Looks like I have some reading to do, but at least its a well defined concept. it just seems so janky&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 21:10:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627332#M185165</guid>
      <dc:creator>weg</dc:creator>
      <dc:date>2020-02-25T21:10:29Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627333#M185166</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Please take note that:&lt;/P&gt;
&lt;P&gt;1. Each SET statement uses its own internal pointer.&lt;/P&gt;
&lt;P&gt;2. _N_ is a data step iteration counter.&lt;/P&gt;
&lt;P&gt;This should answer your questions and explain what you are seeing. Tell us if it doesn't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You left out the fact that most SAS data step stop in the middle of the code when they attempt to read past the end of one of the inputs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 21:17:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627333#M185166</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-25T21:17:14Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627336#M185168</link>
      <description>&lt;P&gt;Too many questions in one post.&amp;nbsp; Let's look at your first example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input name :$13. rate @@;
datalines;
A 1 B 2 C 3
;

data test2;
  set test;
  do var=1 to 2;
    set test;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So when _N_=1 you read the first observation.&amp;nbsp; Then in the loop your read the first two observations and write them.&lt;/P&gt;
&lt;P&gt;On _N_=2 you read the second observation.&amp;nbsp; Then in the loop you read the third observations and write. Then attempt to read a fourth observation and run out of data so the step ends.&amp;nbsp; Check the log the notes will show this.&lt;/P&gt;
&lt;PRE&gt;NOTE: There were 2 observations read from the data set WORK.TEST.
NOTE: There were 3 observations read from the data set WORK.TEST.
NOTE: The data set WORK.TEST2 has 3 observations and 3 variables.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 21:28:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627336#M185168</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-25T21:28:33Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627341#M185172</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Q2&amp;nbsp;&amp;nbsp; It kind of bothers me that set changes its meaning inside of a do statement ( the idea seems similar to subsetting if statements in that some code is changing the behavior other code without referencing it), so I'd love some reason I can give myself for why that is rational behavior, and what class of other sas commands I can expect to change behaviors inside of something else.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I have no idea what this question is about. The meaning of a SET statement does not change. It means read an observation from a dataset (or series of datasets).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 21:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627341#M185172</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-25T21:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627343#M185173</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Q3 &amp;nbsp;&amp;nbsp; Is my idea right that everything hangs on some internal _N_ counter? using set multiple times doesnt go to the next row, so it has to be some inherent property of what is calling it that indicates what row set will access.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Nothing depends on the _N_ counter.&amp;nbsp; SAS just increments that variable every time the data step iterates. You can use it if you want, or just ignore it, which is the normal thing to do.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 21:53:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627343#M185173</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-25T21:53:52Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627344#M185174</link>
      <description>&lt;P&gt;OK, for now let's stick to the first question.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This part of your understanding is not quite right:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;so my understanding is the data step starts its iteration _N_= 1, gets to the do loop and&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Actually, the DATA step does start with _N_=1.&amp;nbsp; It does not get to the DO loop.&amp;nbsp; It gets to the first SET statement, and after that gets to the DO loop.&amp;nbsp; The first SET statement reads the first observation.&amp;nbsp; Inside the DO loop, the second SET statement reads that first observation over again (when VAR=1) then reads the second observation (when VAR=2).&amp;nbsp; Because of the OUTPUT statement, both observations get output.&amp;nbsp; Notice that the SET statements operate independently of one another, both beginning with the first observation.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;At the end of the programming statements, SAS returns to the top of the DATA step and performs those statements over again.&amp;nbsp; It gets to the first SET statement, which now reads the second observation.&amp;nbsp; Then it enters the DO loop, which reads the third observation (and outputs it) and looks for a fourth observation.&amp;nbsp; Since there is no fourth observation, the DATA step is over.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Start there, see if this starts to make sense.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 21:55:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627344#M185174</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-02-25T21:55:50Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627346#M185175</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Q4&amp;nbsp;&amp;nbsp;&amp;nbsp; say I wanted to use set and a do loop from the 50th to 60th observation.&amp;nbsp; Is there a nice way to do that instead of some if statement while iterating through every observation?&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;It depends on what you mean by the question.&amp;nbsp; If you want your SET sattement to only read from the 50th to 60th observation use the firstobs= and obs= dataset options.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set one (firstobs=50 obs=60);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;The SET statement will work as it always work but the first time it execute it skips the first 49 observations.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If you really wanted to do that in a DO loop then use the POINT= option of the SET statement instead.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do obs_num=50 to 60;
  set one point=obs_num ;
  ...
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;But take care that you have a way for your data step to end.&amp;nbsp; The normal way a data step ends is when SAS reads past the end of the input data (caused by execution of a SET/MERGE/UPDATE statement or an INPUT statement).&amp;nbsp; If you use the POINT= option then the SET will never read past the end.&amp;nbsp; It might error out if you tried to point too far, but in this example if the dataset has at least 60 observations then you are at risk of writing an infinite loop.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 21:59:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627346#M185175</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-25T21:59:53Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627364#M185183</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt;&amp;nbsp;By internal pointer, I am assuming that it works the same way as _N_? and it only is thrown out when the data step completes?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The internal pointer is invisible to you. It is used each time a SET statement is executed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some actually requested that it be made public, is some form.&amp;nbsp; &lt;A href="https://communities.sas.com/t5/SASware-Ballot-Ideas/Allow-to-use-observation-s-number-curobs-in-the-where-clause-and/idi-p/589311" target="_self"&gt;Here&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 01:08:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627364#M185183</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-02-26T01:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627365#M185184</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt;&amp;nbsp;You left out the fact that most SAS data step stop in the middle of the code when they attempt to read past the end of one of the inputs.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Indeed that's a relevant fact. Add that as point 3.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 01:09:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627365#M185184</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-02-26T01:09:21Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627440#M185235</link>
      <description>&lt;P&gt;Isn't curobs= enough to show the current obs pointer for a given set statement?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 08:45:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627440#M185235</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-02-26T08:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627465#M185250</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These are really EXCELLENT questions, especially for a beginner.&amp;nbsp; I wish I had asked these questions earlier in my SAS programming.&amp;nbsp; It took me a few years to really understand the DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Importantly, your general understanding is wrong:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;My general understanding of `set lib.table` is that it reads the _N_'th observation from the&amp;nbsp; given table where _N_ is the PDV's internal counter.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The DATA step is an implied loop.&amp;nbsp; _N_ is simply a counter of the number of times the loop has executed.&amp;nbsp; There is no causal relationship / dependency between _N_ and the SET statement.&amp;nbsp; When the SET statement executes, it reads the next record from a data set.&amp;nbsp; What does "next record" mean? The SET statement has its own pointer which tracks which record to read from a data set.&amp;nbsp; There is often a correlation between _N_ and the SET statement because many steps happen to execute the SET statement once for each iteration of the DATA step.&amp;nbsp; But that is just a correlation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The below step iterates 20 times.&amp;nbsp; The log shows that on each iteration of the DATA step, one record is read (and output):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  put "Top of loop " _N_= ;
  set sashelp.class ;
  put "Bottom of loop " _N_= Name= ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On the 20th iteration of the loop, the SET statement executes.&amp;nbsp; Because there is no next record to read, it hits the end of file marker and that causes the DATA step to stop executing.&amp;nbsp; Note that the "Bottom of loop" PUT statement does not execute on the 20th iteration, because the step stopped executing when the SET statement executed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The below step iterates only four times, but also reads and outputs all 19 records from sashelp.class.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  put "Top of loop " _N_= ;
  do i=1 to 5 ;
    set sashelp.class ;
    output ;
    put "Inside DO loop " _N_= i= Name= ;
  end ;
  put "Bottom of loop " _N_= Name= /;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On the first iteration of the DATA step (_N_=1), the explicit do loop iterates 5 times, so the SET statement reads the first 5 records.&amp;nbsp; On the second iteration of the DATA step (_N_=2), the explicit do loop iterates 5 times, so the SET statement reads records 6-10.&amp;nbsp; On the 4th iteration of the DATA step (_N_=4), the explicit do loop iterates 5 times.&amp;nbsp; On i=1 to i=4, the SET statement reads records 16-19.&amp;nbsp; When i=5, the SET statement tries to read the next record, it hits the end of file marker, and the DATA step stops executing immediately.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In answer to other questions:&lt;/P&gt;
&lt;P&gt;1. The SET statement does NOT change its meaning inside of a DO loop.&amp;nbsp; That would be chaos if it did.&amp;nbsp; If this is not clear, please post an example where you think this is happening.&lt;/P&gt;
&lt;P&gt;2. Each SET statement that is reading from a data set uses its own internal pointer to keep track of which record to read.&amp;nbsp; If there are two SET statements in a step, each still has its own pointer, and the two SET statements are independent of each other.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To work through your second example (sorry, I'm out of time to write more), I would recommend you add some PUT statements, something like below, and remember that each SET statement has it's own pointer, the two SET statements are independent of each other, and there is only one PDV for the step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
  length name $13 ;
  put "Top of DATA step " (_N_ Var Name Rate)(=) ;
  set test;
  put "Before DO loop " (_N_ Var Name Rate)(=) ;

  do var=1 to 2;
    put "Top of DO loop "  (_N_ Var Name Rate)(=) ; 
    set test;
    output;
    put "Bottom of DO loop "  (_N_ Var Name Rate)(=) ; 
  end;
  put "Bottom of DATA step " (_N_ Var Name Rate)(=)  /;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can't figure out what is happening, respond with more questions. I or others will happily explain more.&amp;nbsp; These are good questions, and will raise a lot of issues critical to understanding DATA step programming.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 13:14:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627465#M185250</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-02-26T13:14:03Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627551#M185295</link>
      <description>&lt;P&gt;I have a lot better understanding of whats happening due to everyone's answers. The idea of having an internal counter tripped me up, and it was not expected behavior&amp;nbsp; that the set statement would have it.&amp;nbsp; I expected&amp;nbsp;&lt;EM&gt;do i=1 to 10; set table; output;&amp;nbsp; end;&lt;/EM&gt; to call set 10 times and write whatever the _N_th observation was 10 times to the output table.&amp;nbsp; Also, the fact that the internal pointer is for each set statement and seems to last until the data step ends is different than say in python, where the counter only lasts until the do loop finishes.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate everyone help&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 15:47:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627551#M185295</guid>
      <dc:creator>weg</dc:creator>
      <dc:date>2020-02-26T15:47:41Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627554#M185297</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/309205"&gt;@weg&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a lot better understanding of whats happening due to everyone's answers. The idea of having an internal counter tripped me up, and it was not expected behavior&amp;nbsp; that the set statement would have it.&amp;nbsp; I expected&amp;nbsp;&lt;EM&gt;do i=1 to 10; set table; output;&amp;nbsp; end;&lt;/EM&gt; to call set 10 times and write whatever the _N_th observation was 10 times to the output table.&amp;nbsp; Also, the fact that the internal pointer is for each set statement and seems to last until the data step ends is different than say in python, where the counter only lasts until the do loop finishes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate everyone help&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;We used to call languages like SAS 4GL, fourth generation languages. As opposed to machine language, symbolic assembly languages or programming languages (like FORTRAN or COBOL).&amp;nbsp; You are trying to think of the data step language as if it was one of those lower level languages.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It helps to think it terms of the data step operating on the whole input instead of thinking you have to program the loop over the input.&amp;nbsp; More like the set operation logic of SQL code.&amp;nbsp; Or more like the object oriented concepts of modern languages.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Although as your examples show you can get the data step to operate at the lower level of detail if you have to.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 16:01:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627554#M185297</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-26T16:01:35Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627557#M185298</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It helps to think it terms of the data step operating on the whole input instead of thinking you have to program the loop over the input.&amp;nbsp; More like the set operation logic of SQL code.&amp;nbsp; Or more like the object oriented concepts of modern languages.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I'm really surprised you would say that, Tom.&amp;nbsp; When I first learned SAS, I didn't think about the DATA step as iterating, and didn't think of the PDV.&amp;nbsp; So when I tried to learn about what the RETAIN statement does, or how the MERGE statement works, or BY group processing, or LAG, or ... it was an almost hopeless exercise.&amp;nbsp; I've always thought that in order to use the DATA step, it was essential to understand how it iterates through data as it reads, which is very different than the set operation logic of SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You really find it helpful to think of the DATA step as operating on a set, rather than iterating over it?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 16:17:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627557#M185298</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-02-26T16:17:55Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding behaviour of  set statement inside do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627566#M185304</link>
      <description>&lt;P&gt;Absolutely it helps.&amp;nbsp; Take the example question about find the 50th observation.&amp;nbsp; That is a question about what input should be given to the data step. So it should be solved by identifying the data by the keys in the data, not its position. So first response should be to subset the data on the way into the data step with a WHERE statement or WHERE= dataset option.&amp;nbsp; &amp;nbsp;And if your data model is poor and you did need to resort filtering it by position in the dataset then by the dataset options OBS= and FIRSTOBS=. No need to begin by thinking have to open file, initialize pointer, read from pointer, increment pointer, ... what ever low level operations that actually have to happen. SAS has happily shielded you from having to think about that stuff.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said all of the quirky "features" of the datastep (retain, 1 to many merge, many to many merge) can be explained more clearly once you understand how the data step actually operates and what each of the individual statements actually do.&amp;nbsp; But not is NOT the first way to approach how to use a data step to perform a task.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 16:33:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Understanding-behaviour-of-set-statement-inside-do-loops/m-p/627566#M185304</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-26T16:33:45Z</dc:date>
    </item>
  </channel>
</rss>

