<?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: if and where statement for _n_ in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/if-and-where-statement-for-n/m-p/237677#M268280</link>
    <description>&lt;P&gt;You can do it in a where statement (or where option) if you use the &lt;STRONG&gt;monotonic()&lt;/STRONG&gt; function. The function&amp;nbsp;behaves in a roughly comparable manner to that of&amp;nbsp;&lt;STRONG&gt;_n_&lt;/STRONG&gt;. It even works in PROC SQL, which disallows the variable&amp;nbsp;&lt;STRONG&gt;_n_&lt;/STRONG&gt;!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Enjoy.&amp;nbsp;&lt;img id="robothappy" class="emoticon emoticon-robothappy" src="https://communities.sas.com/i/smilies/16x16_robot-happy.png" alt="Robot Happy" title="Robot Happy" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
  LENGTH name $10;
  name='Jack';   id=555; output;
  name='Jill';   id=333; output;
  name='Bonnie'; id=777; output;
  name='Clyde';  id=123; output;
RUN;

/* version 1: PROC SQL */
PROC SQL;
  CREATE TABLE want1 AS 
  SELECT * 
  FROM have
  WHERE MOD(MONOTONIC(), 2) = 0;&lt;BR /&gt;  /* beware: avoid including an ORDER BY statement here */&lt;BR /&gt;  /*         because the results will be misleading */
QUIT;

/* version 2: data step WHERE */
DATA want2;
  SET have;
  WHERE MOD(MONOTONIC(), 2) = 0;
RUN;

/* version 3: data step WHERE option */
DATA want3;
  SET have(WHERE=(MOD(MONOTONIC(), 2) = 0));
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 03 Dec 2015 19:31:51 GMT</pubDate>
    <dc:creator>hbi</dc:creator>
    <dc:date>2015-12-03T19:31:51Z</dc:date>
    <item>
      <title>if and where statement for _n_</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-and-where-statement-for-n/m-p/237647#M268277</link>
      <description>&lt;P&gt;Hi I am tring to keep all even records for a data and using the where mod(_n_,2) = 0, but it does not work. However if works.&lt;/P&gt;&lt;P&gt;Does that mean _n_ is actaully for the data statment instead of set statment?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 18:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-and-where-statement-for-n/m-p/237647#M268277</guid>
      <dc:creator>yym</dc:creator>
      <dc:date>2015-12-03T18:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: if and where statement for _n_</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-and-where-statement-for-n/m-p/237650#M268278</link>
      <description>&lt;P&gt;WHERE operates differently than IF. &amp;nbsp;WHERE examines the observation before reading it in ... so any fields that you refer to must be part of the incoming data set when using a WHERE statement. &amp;nbsp;That would exclude _n_ which is not part of the data set but is only created when the DATA step executes.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 18:14:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-and-where-statement-for-n/m-p/237650#M268278</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-12-03T18:14:33Z</dc:date>
    </item>
    <item>
      <title>Re: if and where statement for _n_</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-and-where-statement-for-n/m-p/237660#M268279</link>
      <description>&lt;P&gt;Under conditions such as automatic variable, new variable created within data step, first. by, last. by, if must be used, _n_ is automatic variable produced in data step, so you must use if.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 18:31:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-and-where-statement-for-n/m-p/237660#M268279</guid>
      <dc:creator>slchen</dc:creator>
      <dc:date>2015-12-03T18:31:54Z</dc:date>
    </item>
    <item>
      <title>Re: if and where statement for _n_</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-and-where-statement-for-n/m-p/237677#M268280</link>
      <description>&lt;P&gt;You can do it in a where statement (or where option) if you use the &lt;STRONG&gt;monotonic()&lt;/STRONG&gt; function. The function&amp;nbsp;behaves in a roughly comparable manner to that of&amp;nbsp;&lt;STRONG&gt;_n_&lt;/STRONG&gt;. It even works in PROC SQL, which disallows the variable&amp;nbsp;&lt;STRONG&gt;_n_&lt;/STRONG&gt;!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Enjoy.&amp;nbsp;&lt;img id="robothappy" class="emoticon emoticon-robothappy" src="https://communities.sas.com/i/smilies/16x16_robot-happy.png" alt="Robot Happy" title="Robot Happy" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
  LENGTH name $10;
  name='Jack';   id=555; output;
  name='Jill';   id=333; output;
  name='Bonnie'; id=777; output;
  name='Clyde';  id=123; output;
RUN;

/* version 1: PROC SQL */
PROC SQL;
  CREATE TABLE want1 AS 
  SELECT * 
  FROM have
  WHERE MOD(MONOTONIC(), 2) = 0;&lt;BR /&gt;  /* beware: avoid including an ORDER BY statement here */&lt;BR /&gt;  /*         because the results will be misleading */
QUIT;

/* version 2: data step WHERE */
DATA want2;
  SET have;
  WHERE MOD(MONOTONIC(), 2) = 0;
RUN;

/* version 3: data step WHERE option */
DATA want3;
  SET have(WHERE=(MOD(MONOTONIC(), 2) = 0));
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Dec 2015 19:31:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-and-where-statement-for-n/m-p/237677#M268280</guid>
      <dc:creator>hbi</dc:creator>
      <dc:date>2015-12-03T19:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: if and where statement for _n_</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-and-where-statement-for-n/m-p/237999#M268281</link>
      <description>&lt;P&gt;Seems like the undocumented monotonic() function simply counts the number of times it is called, which can give it a value other than _n_&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set sashelp.class(obs=3);
/* Many calls on each data step iteration */
do i =  1 to 2; n = monotonic(); m = _n_; end;
/* Skipped calls on some data step iterations */
if mod(_n_,2)=1 then do; r = monotonic(); s = _n_; end;
put (n m r s) (=);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 06 Dec 2015 05:02:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-and-where-statement-for-n/m-p/237999#M268281</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-12-06T05:02:39Z</dc:date>
    </item>
  </channel>
</rss>

