<?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 SAS _n_ for index array keyword looping in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-n-for-index-array-keyword-looping/m-p/643746#M192169</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data JE.KeywordMatchTemp1;
  if _n_ = 1 then do;
    do i = 1 by 1 until (eof);
      set JE.KeyWords end=eof;
      array keywords[100] $30 _temporary_;
      keywords[i] = Key_Words;
    end;
    last_i = i ;
    retain last_i ;
  end;
  set JE.JEMasterTemp;
  match = 0;
  do i = 1 to last_i while (match=0) ;
    if index(descr, trim(keywords[i]) ) then match = 1;
  end;
  drop i last_i;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi all, I am having difficulty to understand what does this line does..&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if _n_ = 1 then do;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I understand that it creates a field of auto increment by 1 for each records in the dataset.&lt;/P&gt;&lt;P&gt;I have tried to remove this statement and logically speaking the code should run fine too right without this statement?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Instead I am getting obs capping of 1 at this set statement.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; set JE.JEMasterTemp;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need help...&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;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 29 Apr 2020 01:11:57 GMT</pubDate>
    <dc:creator>vanness145</dc:creator>
    <dc:date>2020-04-29T01:11:57Z</dc:date>
    <item>
      <title>SAS _n_ for index array keyword looping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-n-for-index-array-keyword-looping/m-p/643746#M192169</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data JE.KeywordMatchTemp1;
  if _n_ = 1 then do;
    do i = 1 by 1 until (eof);
      set JE.KeyWords end=eof;
      array keywords[100] $30 _temporary_;
      keywords[i] = Key_Words;
    end;
    last_i = i ;
    retain last_i ;
  end;
  set JE.JEMasterTemp;
  match = 0;
  do i = 1 to last_i while (match=0) ;
    if index(descr, trim(keywords[i]) ) then match = 1;
  end;
  drop i last_i;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi all, I am having difficulty to understand what does this line does..&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if _n_ = 1 then do;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I understand that it creates a field of auto increment by 1 for each records in the dataset.&lt;/P&gt;&lt;P&gt;I have tried to remove this statement and logically speaking the code should run fine too right without this statement?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Instead I am getting obs capping of 1 at this set statement.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; set JE.JEMasterTemp;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need help...&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;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 01:11:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-n-for-index-array-keyword-looping/m-p/643746#M192169</guid>
      <dc:creator>vanness145</dc:creator>
      <dc:date>2020-04-29T01:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: SAS _n_ for index array keyword looping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-n-for-index-array-keyword-looping/m-p/643747#M192170</link>
      <description>&lt;P&gt;The data step is an implicit loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS creates an automatic variable _N_, which is a counter for that loop (iteration number).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the code&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if _N_=1 then do ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Tests whether _N_=1 , i.e. whether it is the first iteration of the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your example, on the first iteration of the data step, _N_=1 is true, therefore processing continues into the loop and all records from JE.KeyWords are read into a temporary array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, still on _N_=1 control continues to the second SET statement and the first record is read from JE.JEMasterTemp, and it checks the value of DESCR to see if any of the keywords stored in the array appear in DESCR.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At the bottom of the DATA step, it outputs that first record to KE.KeywordMatchTemp1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On the second iteration of the loop, _N_=2, so the first IF statement is false. Nothing is read from JE.KeyWords.&amp;nbsp; Control continues to read the second record from JE.JEMasterTemp,&amp;nbsp;and it checks the value of DESCR to see if any of the keywords stored in the array appear in DESCR, and outputs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The DATA step keeps looping until the second SET statement tries to read another record from&amp;nbsp;JE.JEMasterTemp but it can't because there are no more records, at which point it reads the (logical) end of file marker and stops.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The point of if _N_=1 in your code is to say that SAS only needs to read the key words into the array once.&amp;nbsp; Temporary arrays are automatically retained.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you remove the if _N_=1, then on the second iteration of the DATA step SAS will try to read a record from JE.KeyWords, but since it has has already read all of the records, the SET statement will read the (logical) end of file marker and the DATA step will stop prematurely (without a warning or note).&amp;nbsp; Because one of the rules of the DATA step is that it stops when a SET satement reads in and end of file marker.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 01:32:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-n-for-index-array-keyword-looping/m-p/643747#M192170</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-04-29T01:32:30Z</dc:date>
    </item>
    <item>
      <title>Re: SAS _n_ for index array keyword looping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-n-for-index-array-keyword-looping/m-p/643765#M192177</link>
      <description>&lt;P&gt;This program loads table KEYWORDS into an array when the data step runs its first iteration.&lt;/P&gt;
&lt;P&gt;Then for each iteration (of the data set MASTERTEMP) it tries to find a match.&lt;/P&gt;
&lt;P&gt;If you try to load KEYWORDS each time:&lt;/P&gt;
&lt;P&gt;1) it is wasteful&lt;/P&gt;
&lt;P&gt;2) SAS stops, as you have seen, as you try to read past the last observation of KEYWORDS on the second iteration.&lt;/P&gt;
&lt;P&gt;So the&amp;nbsp; &amp;nbsp;&lt;FONT face="courier new,courier"&gt;if _N_=1&amp;nbsp;&lt;/FONT&gt; &amp;nbsp;test is very necessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;  do i = 1 to last_i while (match=0) ;
    if index(descr, trim(keywords[i]) ) then match = 1;
  end;
&lt;/LI-CODE&gt;
&lt;P&gt;can be replaced with&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;  MATCH = whichc( DESCR, of KEYWORDS[*] ) &amp;gt; 0 ;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 05:33:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-n-for-index-array-keyword-looping/m-p/643765#M192177</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-04-29T05:33:27Z</dc:date>
    </item>
    <item>
      <title>Re: SAS _n_ for index array keyword looping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-n-for-index-array-keyword-looping/m-p/643769#M192181</link>
      <description>&lt;P&gt;Aside from what the others said, today we would probably make use of a hash object:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data JE.KeywordMatchTemp1;
if _n_ = 1
then do;
  declare hash kw (dataset:"je.keywords");
  kw.definekey("key_words");
  kw.definedone();
end;
set JE.JEMasterTemp;
match = 0;
do i = 1 to countw(descr) while (match = 0);
  if kw.check(key:scan(descr,i)) then match = 1;
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Apr 2020 06:06:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-n-for-index-array-keyword-looping/m-p/643769#M192181</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-29T06:06:13Z</dc:date>
    </item>
    <item>
      <title>Re: SAS _n_ for index array keyword looping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-n-for-index-array-keyword-looping/m-p/644130#M192358</link>
      <description>Thank you so much for the explanation!</description>
      <pubDate>Thu, 30 Apr 2020 02:05:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-n-for-index-array-keyword-looping/m-p/644130#M192358</guid>
      <dc:creator>vanness145</dc:creator>
      <dc:date>2020-04-30T02:05:11Z</dc:date>
    </item>
  </channel>
</rss>

