<?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 if _N_=1 then set &amp;lt;dataset&amp;gt; question; in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/if-N-1-then-set-lt-dataset-gt-question/m-p/42119#M8643</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A nice explanation of how the datastep works can be found in the following paper by Ian Whitlock:&lt;/P&gt;&lt;P&gt;&lt;A href="http://www2.sas.com/proceedings/sugi31/246-31.pdf"&gt;http://www2.sas.com/proceedings/sugi31/246-31.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In that paper, Ian give a much better explanation than I could in this brief response.&amp;nbsp; A simplified explanation for the "if _N_ eq 1" case, is that the datastep has to start the second iteration in order to know that it is no longer the first iteration.&amp;nbsp; You get the second copy of the first record because the value from the first record are automatically retained.and, since it is in its second iteration, _n_ is incremented to 2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the _N_ &amp;lt;= 100 case, since you really only have one record, SAS reaches the end of file normally and simply exits normally.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the _N_ eq 2 case, the variable names and formats are read before the first iteration begins.&amp;nbsp; However, the code immediately exits because the implied loop was interrupted.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 06 Nov 2011 04:29:38 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2011-11-06T04:29:38Z</dc:date>
    <item>
      <title>if _N_=1 then set &lt;dataset&gt; question;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-N-1-then-set-lt-dataset-gt-question/m-p/42118#M8642</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello all, I've been stocked with this problem.&amp;nbsp; When I run a statement that &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;"&lt;/P&gt;&lt;P&gt;data five;&lt;/P&gt;&lt;P&gt;if _N_=1 then set &amp;lt;data1&amp;gt;; "&lt;/P&gt;&lt;P&gt;The output shows me 2 records.&amp;nbsp; looks like this in the following,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;obs vb1&amp;nbsp;&amp;nbsp; vb2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; vb3&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.28&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.28&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4 &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am so confused that if the _N_=1 indicates the first observation in a data set, shouldn't I have only one observation in the output only?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A extended quesion,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;since I wanted to try this problem out, I put if _N_=2 if _N_&amp;lt;=100 to replace my statement above,&lt;/P&gt;&lt;P&gt;in _N_=2 case, it shows only the first observation with the variable names from data1 but appear missing values for all the numbers.&lt;/P&gt;&lt;P&gt;obs vb1&amp;nbsp;&amp;nbsp; vb2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; vb3&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;while &lt;/P&gt;&lt;P&gt;in _N_&amp;lt;=100 case, the output shows one observation with correct number copied;&lt;/P&gt;&lt;P&gt;obs vb1&amp;nbsp;&amp;nbsp; vb2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; vb3&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp; 1.28&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Both of the outputs are unexpected.&amp;nbsp; Please help me with the SAS logic.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you very much for solving my problem.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 06 Nov 2011 02:14:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-N-1-then-set-lt-dataset-gt-question/m-p/42118#M8642</guid>
      <dc:creator>Lulus</dc:creator>
      <dc:date>2011-11-06T02:14:56Z</dc:date>
    </item>
    <item>
      <title>if _N_=1 then set &lt;dataset&gt; question;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-N-1-then-set-lt-dataset-gt-question/m-p/42119#M8643</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A nice explanation of how the datastep works can be found in the following paper by Ian Whitlock:&lt;/P&gt;&lt;P&gt;&lt;A href="http://www2.sas.com/proceedings/sugi31/246-31.pdf"&gt;http://www2.sas.com/proceedings/sugi31/246-31.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In that paper, Ian give a much better explanation than I could in this brief response.&amp;nbsp; A simplified explanation for the "if _N_ eq 1" case, is that the datastep has to start the second iteration in order to know that it is no longer the first iteration.&amp;nbsp; You get the second copy of the first record because the value from the first record are automatically retained.and, since it is in its second iteration, _n_ is incremented to 2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the _N_ &amp;lt;= 100 case, since you really only have one record, SAS reaches the end of file normally and simply exits normally.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the _N_ eq 2 case, the variable names and formats are read before the first iteration begins.&amp;nbsp; However, the code immediately exits because the implied loop was interrupted.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 06 Nov 2011 04:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-N-1-then-set-lt-dataset-gt-question/m-p/42119#M8643</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2011-11-06T04:29:38Z</dc:date>
    </item>
    <item>
      <title>if _N_=1 then set &lt;dataset&gt; question;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-N-1-then-set-lt-dataset-gt-question/m-p/42120#M8644</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you very much for help.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 06 Nov 2011 19:37:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-N-1-then-set-lt-dataset-gt-question/m-p/42120#M8644</guid>
      <dc:creator>Lulus</dc:creator>
      <dc:date>2011-11-06T19:37:06Z</dc:date>
    </item>
    <item>
      <title>Re: if _N_=1 then set &lt;dataset&gt; question;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-N-1-then-set-lt-dataset-gt-question/m-p/390772#M93783</link>
      <description>&lt;P&gt;&lt;A href="http://www2.sas.com/proceedings/sugi25/25/po/25p222.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi25/25/po/25p222.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;check this out&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 21:38:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-N-1-then-set-lt-dataset-gt-question/m-p/390772#M93783</guid>
      <dc:creator>danielyang1009</dc:creator>
      <dc:date>2017-08-24T21:38:58Z</dc:date>
    </item>
  </channel>
</rss>

