<?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: Conditional statement when no records in dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-statement-when-no-records-in-dataset/m-p/526708#M143498</link>
    <description>&lt;P&gt;Your EOF macro reading in the data set could check for zero observations and report that status to the log, otherwise leaving the data set unmodified.&amp;nbsp; Is that what you are looking for?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example using sashelp.class data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data m f uncoded;
  set sashelp.class;
  if sex='M' then output m;
  else if sex='F' then output f;
  else output uncoded;
run;

%macro eof(sex);
  data &amp;amp;sex;
  if nrecs=0 then do;
     put "No Observations in &amp;amp;sex";
     stop;
  end;
  if eof then output;
  modify &amp;amp;sex nobs=nrecs end=eof;
  run;
%mend eof;

options mprint;
%eof(m);
%eof(f);
%eof(uncoded);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It might seem counterintuitive to have an if-test for NRECS=0&amp;nbsp; preceding the apparent assignment of a value to nrecs in the MODIFY statement.&amp;nbsp; But that's because the assignment of a value to nrecs is done at the data step compiler stage, not the execution stage.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 13 Jan 2019 05:13:36 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2019-01-13T05:13:36Z</dc:date>
    <item>
      <title>Conditional statement when no records in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-statement-when-no-records-in-dataset/m-p/526699#M143493</link>
      <description>&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; montreal baltimore jersey;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; sashelp.baseball(&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;keep&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;= name team nhits);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; nhits &amp;gt;=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;20&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;and&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; nhits &amp;lt;= &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;60&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; team = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'Montreal'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;output&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Montreal;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; team = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'Baltimore'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;output&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Baltimore;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; team = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'Jersey'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;output&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Jersey;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; eof(report);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;data &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;report.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if eof then output;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt; modify &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;report.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; end=eof; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;eof&lt;/I&gt;&lt;/STRONG&gt;(montreal);&lt;/P&gt;
&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;eof&lt;/I&gt;&lt;/STRONG&gt;(baltimore);&lt;/P&gt;
&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;eof&lt;/I&gt;&lt;/STRONG&gt;(jersey);&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case the only dataset that has no records is Jersey.&amp;nbsp; How would I insert a statement in the "Team" variable that says "No Records"&lt;/P&gt;</description>
      <pubDate>Sun, 13 Jan 2019 03:55:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-statement-when-no-records-in-dataset/m-p/526699#M143493</guid>
      <dc:creator>Q1983</dc:creator>
      <dc:date>2019-01-13T03:55:49Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional statement when no records in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-statement-when-no-records-in-dataset/m-p/526708#M143498</link>
      <description>&lt;P&gt;Your EOF macro reading in the data set could check for zero observations and report that status to the log, otherwise leaving the data set unmodified.&amp;nbsp; Is that what you are looking for?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example using sashelp.class data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data m f uncoded;
  set sashelp.class;
  if sex='M' then output m;
  else if sex='F' then output f;
  else output uncoded;
run;

%macro eof(sex);
  data &amp;amp;sex;
  if nrecs=0 then do;
     put "No Observations in &amp;amp;sex";
     stop;
  end;
  if eof then output;
  modify &amp;amp;sex nobs=nrecs end=eof;
  run;
%mend eof;

options mprint;
%eof(m);
%eof(f);
%eof(uncoded);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It might seem counterintuitive to have an if-test for NRECS=0&amp;nbsp; preceding the apparent assignment of a value to nrecs in the MODIFY statement.&amp;nbsp; But that's because the assignment of a value to nrecs is done at the data step compiler stage, not the execution stage.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Jan 2019 05:13:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-statement-when-no-records-in-dataset/m-p/526708#M143498</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-01-13T05:13:36Z</dc:date>
    </item>
  </channel>
</rss>

