<?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 read data 1 times or 2 times when declare hash object with or without &amp;quot;if _n_=1&amp;quot; block? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Why-read-data-1-times-or-2-times-when-declare-hash-object-with/m-p/973810#M377741</link>
    <description>&lt;P&gt;This is an example of submiting hash declare statements with&amp;nbsp;"if _n_=1" block:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then do;
    declare hash h(dataset:"sashelp.class(where=((weight*0.4536)/(height*0.0254)**2&amp;gt;20))");
    h.definekey('age'); 
    h.definedone();
  end;
  do until(_eof_);
    set sashelp.class end=_eof_;
    bmi=(weight*0.4536)/(height*0.0254)**2;
    if h.check()=0 then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SAS log shows the lookup table was read &lt;FONT color="#FF0000"&gt;1 times&lt;/FONT&gt;:&lt;/P&gt;
&lt;PRE&gt;NOTE: There were 5 observations read from the data set SASHELP.CLASS.
      WHERE ((weight*0.4536)/((height*0.0254)**2))&amp;gt;20;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.WANT has 10 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Removing the "if _n_=1" block":&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  declare hash h(dataset:"sashelp.class(where=((weight*0.4536)/(height*0.0254)**2&amp;gt;20))");
  h.definekey('age'); 
  h.definedone();
  do until(_eof_);
    set sashelp.class end=_eof_;
    bmi=(weight*0.4536)/(height*0.0254)**2;
    if h.check()=0 then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;SAS log shows the lookup table was read &lt;FONT color="#FF0000"&gt;2 times&lt;/FONT&gt;:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;NOTE: There were 5 observations read from the data set SASHELP.CLASS.
      WHERE ((weight*0.4536)/((height*0.0254)**2))&amp;gt;20;
NOTE: There were 5 observations read from the data set SASHELP.CLASS.
      WHERE ((weight*0.4536)/((height*0.0254)**2))&amp;gt;20;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.WANT has 10 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;What makes that difference?&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 01 Sep 2025 08:05:14 GMT</pubDate>
    <dc:creator>whymath</dc:creator>
    <dc:date>2025-09-01T08:05:14Z</dc:date>
    <item>
      <title>Why read data 1 times or 2 times when declare hash object with or without "if _n_=1" block?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-read-data-1-times-or-2-times-when-declare-hash-object-with/m-p/973810#M377741</link>
      <description>&lt;P&gt;This is an example of submiting hash declare statements with&amp;nbsp;"if _n_=1" block:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then do;
    declare hash h(dataset:"sashelp.class(where=((weight*0.4536)/(height*0.0254)**2&amp;gt;20))");
    h.definekey('age'); 
    h.definedone();
  end;
  do until(_eof_);
    set sashelp.class end=_eof_;
    bmi=(weight*0.4536)/(height*0.0254)**2;
    if h.check()=0 then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SAS log shows the lookup table was read &lt;FONT color="#FF0000"&gt;1 times&lt;/FONT&gt;:&lt;/P&gt;
&lt;PRE&gt;NOTE: There were 5 observations read from the data set SASHELP.CLASS.
      WHERE ((weight*0.4536)/((height*0.0254)**2))&amp;gt;20;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.WANT has 10 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Removing the "if _n_=1" block":&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  declare hash h(dataset:"sashelp.class(where=((weight*0.4536)/(height*0.0254)**2&amp;gt;20))");
  h.definekey('age'); 
  h.definedone();
  do until(_eof_);
    set sashelp.class end=_eof_;
    bmi=(weight*0.4536)/(height*0.0254)**2;
    if h.check()=0 then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;SAS log shows the lookup table was read &lt;FONT color="#FF0000"&gt;2 times&lt;/FONT&gt;:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;NOTE: There were 5 observations read from the data set SASHELP.CLASS.
      WHERE ((weight*0.4536)/((height*0.0254)**2))&amp;gt;20;
NOTE: There were 5 observations read from the data set SASHELP.CLASS.
      WHERE ((weight*0.4536)/((height*0.0254)**2))&amp;gt;20;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.WANT has 10 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;What makes that difference?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Sep 2025 08:05:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-read-data-1-times-or-2-times-when-declare-hash-object-with/m-p/973810#M377741</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2025-09-01T08:05:14Z</dc:date>
    </item>
    <item>
      <title>Re: Why read data 1 times or 2 times when declare hash object with or without "if _n_=1" b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-read-data-1-times-or-2-times-when-declare-hash-object-with/m-p/973816#M377743</link>
      <description>&lt;P&gt;This has nothing to do with the hash table definition but with the way you're using the set statement. The data step will iterate twice in such a scenario.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1756715523184.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109500i9CFD60792FE07A01/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1756715523184.png" alt="Patrick_0-1756715523184.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Add a STOP statement at the end of your data step to avoid this behaviour.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1756715602917.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109501iFAC933904E44FABC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1756715602917.png" alt="Patrick_1-1756715602917.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Sep 2025 08:34:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-read-data-1-times-or-2-times-when-declare-hash-object-with/m-p/973816#M377743</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2025-09-01T08:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: Why read data 1 times or 2 times when declare hash object with or without "if _n_=1" b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-read-data-1-times-or-2-times-when-declare-hash-object-with/m-p/973820#M377744</link>
      <description>Thank you. I am shocked, does that means the supervisor will return control to the top of data step, after running DOW-Loop? What's the usage of this feature?</description>
      <pubDate>Mon, 01 Sep 2025 09:21:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-read-data-1-times-or-2-times-when-declare-hash-object-with/m-p/973820#M377744</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2025-09-01T09:21:09Z</dc:date>
    </item>
    <item>
      <title>Re: Why read data 1 times or 2 times when declare hash object with or without "if _n_=1" b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-read-data-1-times-or-2-times-when-declare-hash-object-with/m-p/973824#M377746</link>
      <description>I know this is not hash's business, please see my another post.</description>
      <pubDate>Mon, 01 Sep 2025 09:35:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-read-data-1-times-or-2-times-when-declare-hash-object-with/m-p/973824#M377746</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2025-09-01T09:35:57Z</dc:date>
    </item>
    <item>
      <title>Re: Why read data 1 times or 2 times when declare hash object with or without "if _n_=1" b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-read-data-1-times-or-2-times-when-declare-hash-object-with/m-p/973848#M377755</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270406"&gt;@whymath&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you. I am shocked, does that means the supervisor will return control to the top of data step, after running DOW-Loop? What's the usage of this feature?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is how the data step works.&amp;nbsp; &amp;nbsp;It does not care (or even really know) that you put the SET statement inside a DO loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most data steps are like your first one, they stop when they read past their inputs.&lt;/P&gt;
&lt;PRE&gt;2    data _null_;
3      put _n_= eof= 'BEFORE';
4      set oneobs end=eof;
5      put _n_= eof= 'AFTER';
6    run;

_N_=1 eof=0 BEFORE
_N_=1 eof=1 AFTER
_N_=2 eof=1 BEFORE
NOTE: There were 1 observations read from the data set WORK.ONEOBS.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 second
&lt;/PRE&gt;
&lt;P&gt;But you can explicitly tell it when to stop, like in your second one.&lt;/P&gt;
&lt;PRE&gt;8    data _null_;
9      put _n_= eof= 'BEFORE';
10     set oneobs end=eof;
11     put _n_= eof= 'AFTER';
12     stop;
13   run;

_N_=1 eof=0 BEFORE
_N_=1 eof=1 AFTER
NOTE: There were 1 observations read from the data set WORK.ONEOBS.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;Or if there are no inputs at all then it knows not to start are second iteration.&lt;/P&gt;
&lt;PRE&gt;15   data _null_;
16     put _n_= bmi= 'BEFORE';
17     bmi=(180*0.4536)/(75*0.0254)**2;
18     put _n_= bmi= 'AFTER';
19   run;

_N_=1 bmi=. BEFORE
_N_=1 bmi=22.498604997 AFTER
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;And if there are inputs but you complete an iteration without actually advancing any of the inputs then it stops and says it is because of looping.&lt;/P&gt;
&lt;PRE&gt;21   data _null_;
22     put _n_= eof= 'BEFORE';
23     if _N_=1 then set oneobs end=eof;
24     put _n_= eof= 'AFTER';
25   run;

_N_=1 eof=0 BEFORE
_N_=1 eof=1 AFTER
_N_=2 eof=1 BEFORE
_N_=2 eof=1 AFTER
&lt;FONT size="3"&gt;&lt;STRONG&gt;NOTE: DATA STEP stopped due to looping.
&lt;/STRONG&gt;&lt;/FONT&gt;NOTE: There were 1 observations read from the data set WORK.ONEOBS.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Sep 2025 15:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-read-data-1-times-or-2-times-when-declare-hash-object-with/m-p/973848#M377755</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-09-01T15:26:41Z</dc:date>
    </item>
  </channel>
</rss>

