<?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: Conditionally print empty table or non empty table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492261#M129338</link>
    <description>&lt;P&gt;The reason is that a SAS data step stops when it reads past the end of the input. By have the SET statement first in the data step it never gets to the other statements when the dataset is empty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The trouble with using NOBS (or even NLOBS) variables available via metadata query (or even the NOBS= option on SET statement) is that it does not work for views.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can instead use the END= option on the SET statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if eof then call execute('proc print data=use_this_if_no_obs; run;');
  else call execute('proc print data=tbl_rawData2; run;');
  stop;
  set tbl_rawData2 end=eof;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that this will fail if the input dataset does not exist at all.&amp;nbsp; If you need to handle that case without SAS generating errors then you will need add more logic.&lt;/P&gt;</description>
    <pubDate>Tue, 04 Sep 2018 12:40:11 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-09-04T12:40:11Z</dc:date>
    <item>
      <title>Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492183#M129300</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I am trying again to print a table.&lt;/P&gt;&lt;P&gt;If the table is empty then I want to print a message that there is no data.&lt;/P&gt;&lt;P&gt;I am running 2 cases.&lt;/P&gt;&lt;P&gt;Case1 is when the table is not empty and then we will have a regular print of the table&lt;/P&gt;&lt;P&gt;Case2 is when a table is empty and then we need to print a message that "no Data"&lt;/P&gt;&lt;P&gt;When I run the code then in Case2 I don't get any print. Why???&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Case1*/
data use_this_if_no_obs;
msg = 'No Data';
run;
data tbl_rawData1;
x=1;
run;
data _null_;
  set tbl_rawData1;
  if  nobs=0 then call execute('proc print data=use_this_if_no_obs; run;');
  else call execute('proc print data=tbl_rawData1; run;');
run;

/*Case2*/
data use_this_if_no_obs;
msg = 'No Data';
run;
data tbl_rawData2;
If 0;
run;
data _null_;
  set tbl_rawData2;
  if  nobs=0 then call execute('proc print data=use_this_if_no_obs; run;');
  else call execute('proc print data=tbl_rawData2; run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 05:02:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492183#M129300</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-09-04T05:02:14Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492192#M129304</link>
      <description>&lt;P&gt;There should be a note in the log complaining about "nobs" not being initialized.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have to use the nobs-option in the set-statement, to get the number of observations of the dataset used.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if 0 then set tbl_rawData2 nobs=nobs;
  if  nobs=0 then call execute('proc print data=use_this_if_no_obs; run;');
  else call execute('proc print data=tbl_rawData2; run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I used the "if 0 then" construct to avoid that the dataset is actually loaded and proc print is called only once.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 05:59:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492192#M129304</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-09-04T05:59:57Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492195#M129306</link>
      <description>&lt;P&gt;This code cannot work as intended. The test will never take place&amp;nbsp;if the table is empty (though I am unsure where your NOBS variable comes from).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set tbl_rawData1;
  if  nobs=0 then ... ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is this what you want?&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; _null_&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  if NOBS=0 then ...&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  set TABLE nobs=NOBS;&lt;BR /&gt;&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 20:59:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492195#M129306</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-09-04T20:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492202#M129312</link>
      <description>&lt;P&gt;You already accepted a solution to this issue in &lt;A href="https://communities.sas.com/t5/SAS-Programming/print-if-data-set-is-null/m-p/492046" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/print-if-data-set-is-null/m-p/492046&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Please study the examples given there carefully, and if you use them, take care of the statements and all their elements and the order in which they are written. All of this is important.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 06:37:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492202#M129312</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-04T06:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492205#M129314</link>
      <description>&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;said the code will not work that way. I did understood your problem though. The problem with nobs option is that its not foolproof with empty datasets. the best way to go in this case is the attrn function. here is what you should be doing for attrn to work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data _null_;

&amp;nbsp; &amp;nbsp;dsnid=open('tbl_rawdata1');

&amp;nbsp; &amp;nbsp;if dsnid then nobs=attrn(open('tbl_rawdata1'), 'nobs' );

&amp;nbsp; &amp;nbsp;if nobs=0 then call execute('proc print data=use_this_if_no_obs; run;');

&amp;nbsp; &amp;nbsp;else&amp;nbsp;&amp;nbsp;call execute('proc print data=tbl_rawdata1; run;');

run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 06:47:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492205#M129314</guid>
      <dc:creator>sb51469</dc:creator>
      <dc:date>2018-09-04T06:47:37Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492217#M129321</link>
      <description>&lt;P&gt;Still not sure what is wrong with the code I presented to you?&amp;nbsp; If the table may not exist, then you should have stated that in your question - I can only work with what you post.&amp;nbsp; A simple fix is:&lt;/P&gt;
&lt;PRE&gt;data  use_this_if_no_obs;
  msg='No Data';
run;

data _null_;
  set sashelp.vtable (where=((libname="WORK" and memname="ABC") or (libname="SASHELP" and memname="CARS")));&lt;BR /&gt;  by libname;&lt;BR /&gt;  if (first.libname and last.libname) or (memname="ABC" and nobs=0)&lt;BR /&gt;    then call execute('proc print data=use_this_if_no_obs; run;');
  else call execute('proc print data=abc; run;');
run;&lt;/PRE&gt;
&lt;P&gt;I.e. use a table you know is always there so that something is always returned.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 08:05:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492217#M129321</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-04T08:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492221#M129322</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;, your solution's also fine, but I think this guy is having some problems with SASHELP.VTABLE. But then yeah he should specify more about his problem.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 08:21:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492221#M129322</guid>
      <dc:creator>sb51469</dc:creator>
      <dc:date>2018-09-04T08:21:09Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492228#M129325</link>
      <description>&lt;P&gt;I would put this in a macro, as it sounds like you want to do it more than once:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro print(data);
  %local PrintData;
  %let PrintData=_no_obs_in_data;
  data _no_obs_in_data;
    msg="&amp;amp;data is empty";
    output;
    set &amp;amp;data;
    call symput('PrintData',"&amp;amp;data"); /* only gets executed if there are data */
    keep msg;
  run;
  proc print data=&amp;amp;PrintData;
  run;
%mend;
%print(tbl_rawData1);
    &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Sep 2018 08:55:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492228#M129325</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-09-04T08:55:57Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492234#M129329</link>
      <description>&lt;P&gt;Sorry, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/206587"&gt;@sb51469&lt;/a&gt;, but your solution does not work if all obs have been delete from "tbl_rawdata1".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.tbl_rawdata1;
   set sashelp.class;
run;

proc sql noprint;
   delete from work.tbl_rawdata1
      where Age &amp;lt; 20;
quit;

data use_this_if_no_obs;
   msg = 'No Data';
run;

data _null_;
   dsnid=open('work.tbl_rawdata1');

   if dsnid then do;
      nobs=attrn(dsnid, 'nobs');
   end;

   if nobs=0 then do;
      call execute('proc print data=use_this_if_no_obs; run;');
   end;
   else do;
      call execute('proc print data=work.tbl_rawdata1; run;');
   end;

   rc = close(dsnid);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Replacing "nobs" with "nlobs" in function attrn solves that issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you explain&amp;nbsp; "The problem with nobs option is that its not foolproof with empty datasets. the best way to go in this case is the attrn function." further?&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 09:32:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492234#M129329</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-09-04T09:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492238#M129331</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;,&amp;nbsp;thanks for correcting my mistake.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;On the 'nobs being not a foolproof' comment, actually it occurred with me once while I was creating this report where a&amp;nbsp;dataset didn't had any observations, I was using the nobs= option to find out if it was empty or not and I was always getting 1 as the answer instead of 0. Didn't understood what exactly was the problem there.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I switched on to the nlobs option in attrn and yeah I mentioned it wrongly here as nobs, my bad. Since I work for an institution where I have signed a Non-disclosure, I don't directly copy codes from my EG to here. I just type everything, so typo errors do occur in my reply.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 09:45:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492238#M129331</guid>
      <dc:creator>sb51469</dc:creator>
      <dc:date>2018-09-04T09:45:22Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492261#M129338</link>
      <description>&lt;P&gt;The reason is that a SAS data step stops when it reads past the end of the input. By have the SET statement first in the data step it never gets to the other statements when the dataset is empty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The trouble with using NOBS (or even NLOBS) variables available via metadata query (or even the NOBS= option on SET statement) is that it does not work for views.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can instead use the END= option on the SET statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if eof then call execute('proc print data=use_this_if_no_obs; run;');
  else call execute('proc print data=tbl_rawData2; run;');
  stop;
  set tbl_rawData2 end=eof;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that this will fail if the input dataset does not exist at all.&amp;nbsp; If you need to handle that case without SAS generating errors then you will need add more logic.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 12:40:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492261#M129338</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-09-04T12:40:11Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492266#M129339</link>
      <description>&lt;P&gt;Can you &amp;nbsp;please explain your code?&lt;/P&gt;&lt;P&gt;It is working perfect but I cannot understand it.&lt;/P&gt;&lt;P&gt;I don't understand the following code:&lt;BR /&gt;"if 0 then set tbl_rawData2 nobs=nobs;"&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 12:52:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492266#M129339</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-09-04T12:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492270#M129341</link>
      <description>&lt;P&gt;The set statement has two elements, declarative and executive.&lt;/P&gt;
&lt;P&gt;declarative: it tells the SAS compiler to read the metadata of the dataset and create place for the variables in the PDV. It also sets things like the nobs= variable.&lt;/P&gt;
&lt;P&gt;executive: read an observation from the dataset, until eof is reached.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The statement forces the data step compiler to honor the declarative part, but prevents the running of the executive part.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 13:16:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492270#M129341</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-04T13:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally print empty table or non empty table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492537#M129434</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, that's a good perspective to solve this problem from every angle. Thanks for the solution.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 03:18:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-print-empty-table-or-non-empty-table/m-p/492537#M129434</guid>
      <dc:creator>sb51469</dc:creator>
      <dc:date>2018-09-05T03:18:45Z</dc:date>
    </item>
  </channel>
</rss>

