<?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: When won't _N_ be representing observation number in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467168#M119244</link>
    <description>&lt;P&gt;Did you miss to read the full sentence in that very book?&lt;/P&gt;&lt;P&gt;_n_ indicates the number of times SAS has looped though the datatep. This is not necessarily equal to the observation number(in the output dataset) since simple subsetting IF statement can change the relationship between observation number and number of iterations of the data step&lt;/P&gt;&lt;P&gt;Run the following and notice the difference between Obs and K. Read the above description again after running the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set sashelp.class ;
if sex='M';
k=_n_;
run;
proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 02 Jun 2018 22:26:13 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-06-02T22:26:13Z</dc:date>
    <item>
      <title>When won't _N_ be representing observation number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467166#M119242</link>
      <description>&lt;P&gt;I feel that I'm confused by how _N_ works. When reading the Little SAS book, it mentions that _N_ not necessarily equal to the observation number, can someone list some examples to demonstrate that point ? Thank you !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jun 2018 21:44:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467166#M119242</guid>
      <dc:creator>Alice_2018</dc:creator>
      <dc:date>2018-06-02T21:44:54Z</dc:date>
    </item>
    <item>
      <title>Re: When won't _N_ be representing observation number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467168#M119244</link>
      <description>&lt;P&gt;Did you miss to read the full sentence in that very book?&lt;/P&gt;&lt;P&gt;_n_ indicates the number of times SAS has looped though the datatep. This is not necessarily equal to the observation number(in the output dataset) since simple subsetting IF statement can change the relationship between observation number and number of iterations of the data step&lt;/P&gt;&lt;P&gt;Run the following and notice the difference between Obs and K. Read the above description again after running the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set sashelp.class ;
if sex='M';
k=_n_;
run;
proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Jun 2018 22:26:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467168#M119244</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-02T22:26:13Z</dc:date>
    </item>
    <item>
      <title>Re: When won't _N_ be representing observation number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467171#M119247</link>
      <description>&lt;P&gt;Another example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
do until eof;
  set sashelp.class end=eof;
  number= _n_;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Jun 2018 22:43:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467171#M119247</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-02T22:43:37Z</dc:date>
    </item>
    <item>
      <title>Re: When won't _N_ be representing observation number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467172#M119248</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp; Sir, Oktoberfest miss of parenthesis&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;do until eof&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;do until (eof)&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jun 2018 22:47:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467172#M119248</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-02T22:47:59Z</dc:date>
    </item>
    <item>
      <title>Re: When won't _N_ be representing observation number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467173#M119249</link>
      <description>&lt;P&gt;Yep.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jun 2018 23:14:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467173#M119249</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-02T23:14:08Z</dc:date>
    </item>
    <item>
      <title>Re: When won't _N_ be representing observation number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467180#M119250</link>
      <description>&lt;P&gt;Thank you very much !&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jun 2018 00:40:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467180#M119250</guid>
      <dc:creator>Alice_2018</dc:creator>
      <dc:date>2018-06-03T00:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: When won't _N_ be representing observation number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467181#M119251</link>
      <description>&lt;P&gt;Thanks so much for another example !&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jun 2018 00:41:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467181#M119251</guid>
      <dc:creator>Alice_2018</dc:creator>
      <dc:date>2018-06-03T00:41:23Z</dc:date>
    </item>
    <item>
      <title>Re: When won't _N_ be representing observation number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467223#M119272</link>
      <description>&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; test&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
do until eof&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; sashelp&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token statement"&gt;class&lt;/SPAN&gt; end&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;eof&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  number&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; _n_&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;sorry I did not got it, how come the value of number is always 1, although the whole dataset is being read. Can someone please explain&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jun 2018 14:29:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467223#M119272</guid>
      <dc:creator>ruchi11dec</dc:creator>
      <dc:date>2018-06-03T14:29:48Z</dc:date>
    </item>
    <item>
      <title>Re: When won't _N_ be representing observation number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467310#M119288</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/145839"&gt;@ruchi11dec&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; test&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; do until (eof)&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; sashelp&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token statement"&gt;class&lt;/SPAN&gt; end&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;eof&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; number&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; _n_&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;sorry I did not got it, how come the value of number is always 1, although the whole dataset is being read. Can someone please explain&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Keep in mind that _n_ is incremented only when the data step begins a new iteration.&lt;/P&gt;
&lt;P&gt;What happens here is this:&lt;/P&gt;
&lt;P&gt;At the start of the first data step iteration, _n_ is set to 1.&lt;/P&gt;
&lt;P&gt;Then the do loop executes; since we stay within the first data step iteration, _n_ does not change.&lt;/P&gt;
&lt;P&gt;Then the do loop ends when the reading of the last observation sets eof to true, but we have not yet tried to read &lt;EM&gt;past&lt;/EM&gt; the last observation.&lt;/P&gt;
&lt;P&gt;A new data step iteration begins, _n_ is set to two, and since we have a do until loop, the loop is entered at least once. Now the set statement tries to read past eof, which triggers the (normal)&amp;nbsp;termination of the data step.&lt;/P&gt;
&lt;P&gt;You can verify this by adding a few put statements:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
put 'before loop';
put _n_=;
do until (eof);
  put 'in loop';
  put _n_=;
  set sashelp.class end=eof;
  number= _n_;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A nice variation on the theme is this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
put 'before loop';
put _n_=;
do while (not eof);
  put 'in loop';
  put _n_=;
  set sashelp.class end=eof;
  number= _n_;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since now the data step would never try to read past eof, the data step would iterate endlessly. But there's a "safety valve" built into the data step that recognizes such a condition and stops the step on its own. You see that in the log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;NOTE: DATA STEP stopped due to looping.
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST has 19 observations and 6 variables.
&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Jun 2018 07:12:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467310#M119288</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-04T07:12:50Z</dc:date>
    </item>
    <item>
      <title>Re: When won't _N_ be representing observation number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467356#M119300</link>
      <description>Thank you so much for such a nice explanation &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Mon, 04 Jun 2018 12:04:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-won-t-N-be-representing-observation-number/m-p/467356#M119300</guid>
      <dc:creator>ruchi11dec</dc:creator>
      <dc:date>2018-06-04T12:04:46Z</dc:date>
    </item>
  </channel>
</rss>

