<?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 Macro issue when dataset has 0 observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503583#M134599</link>
    <description>&lt;P&gt;Hi, I created this macro below and have been running it with datasets that contain observations, but as soon as one dataset has 0 observations that fit the criteria, it seems that the macro spits out numbers from the dataset that was run previously. Here is my code below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro num (vorder, var);&lt;BR /&gt;&lt;BR /&gt;data final_&amp;amp;vorder;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; set t5;&lt;BR /&gt;&amp;nbsp;if &amp;amp;var ne .;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;proc summary data=final_&amp;amp;vorder completetypes chartype missing noprint;&lt;BR /&gt;&amp;nbsp;class outcome trt;&lt;BR /&gt;&amp;nbsp;types outcome*trt;&lt;BR /&gt;&amp;nbsp;output out=discrete;&lt;BR /&gt;&amp;nbsp;run;&lt;BR /&gt;&lt;BR /&gt;proc freq data=discrete;&lt;BR /&gt;&amp;nbsp;by _type_;&lt;BR /&gt;&amp;nbsp;tables outcome*trt/norow nopercent;&lt;BR /&gt;&amp;nbsp;weight _freq_/zeros;&lt;BR /&gt;&amp;nbsp;ods output crosstabfreqs_&amp;amp;vorder=crosstabfreqs_&amp;amp;vorder (where=(_type_2='01' or _type_2='11'));&lt;BR /&gt;&amp;nbsp;run;&lt;BR /&gt;&lt;BR /&gt;data CrossTabFreqs_&amp;amp;vorder /*(keep=vname rowlabel cell _level_ idlabel _type_)*/;&lt;BR /&gt;length vname $32 rowlabel $100 cell $32 idlabel $256;&lt;BR /&gt;set CrossTabFreqs;&lt;BR /&gt;vname =vnamex(scan(table,2,' '));&lt;BR /&gt;rowlabel=vvalueX(vname);&lt;BR /&gt;cell=cats(Frequency);&lt;BR /&gt;format colPercent 8.1;&lt;BR /&gt;set bign key=trt_dec/unique;&lt;BR /&gt;idlabel=cat(strip(vvalue(trt_dec)),' ^ ',cat('(N = ',bign,')'));&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to create another criteria that tells SAS to return nothing if there are no observations in the dataset? Thank you, any help is much appreciated!&lt;/P&gt;</description>
    <pubDate>Thu, 11 Oct 2018 22:17:35 GMT</pubDate>
    <dc:creator>kmardinian</dc:creator>
    <dc:date>2018-10-11T22:17:35Z</dc:date>
    <item>
      <title>Macro issue when dataset has 0 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503583#M134599</link>
      <description>&lt;P&gt;Hi, I created this macro below and have been running it with datasets that contain observations, but as soon as one dataset has 0 observations that fit the criteria, it seems that the macro spits out numbers from the dataset that was run previously. Here is my code below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro num (vorder, var);&lt;BR /&gt;&lt;BR /&gt;data final_&amp;amp;vorder;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; set t5;&lt;BR /&gt;&amp;nbsp;if &amp;amp;var ne .;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;proc summary data=final_&amp;amp;vorder completetypes chartype missing noprint;&lt;BR /&gt;&amp;nbsp;class outcome trt;&lt;BR /&gt;&amp;nbsp;types outcome*trt;&lt;BR /&gt;&amp;nbsp;output out=discrete;&lt;BR /&gt;&amp;nbsp;run;&lt;BR /&gt;&lt;BR /&gt;proc freq data=discrete;&lt;BR /&gt;&amp;nbsp;by _type_;&lt;BR /&gt;&amp;nbsp;tables outcome*trt/norow nopercent;&lt;BR /&gt;&amp;nbsp;weight _freq_/zeros;&lt;BR /&gt;&amp;nbsp;ods output crosstabfreqs_&amp;amp;vorder=crosstabfreqs_&amp;amp;vorder (where=(_type_2='01' or _type_2='11'));&lt;BR /&gt;&amp;nbsp;run;&lt;BR /&gt;&lt;BR /&gt;data CrossTabFreqs_&amp;amp;vorder /*(keep=vname rowlabel cell _level_ idlabel _type_)*/;&lt;BR /&gt;length vname $32 rowlabel $100 cell $32 idlabel $256;&lt;BR /&gt;set CrossTabFreqs;&lt;BR /&gt;vname =vnamex(scan(table,2,' '));&lt;BR /&gt;rowlabel=vvalueX(vname);&lt;BR /&gt;cell=cats(Frequency);&lt;BR /&gt;format colPercent 8.1;&lt;BR /&gt;set bign key=trt_dec/unique;&lt;BR /&gt;idlabel=cat(strip(vvalue(trt_dec)),' ^ ',cat('(N = ',bign,')'));&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to create another criteria that tells SAS to return nothing if there are no observations in the dataset? Thank you, any help is much appreciated!&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 22:17:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503583#M134599</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2018-10-11T22:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: Macro issue when dataset has 0 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503584#M134600</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/139341"&gt;@kmardinian&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I created this macro below and have been running it with datasets that contain observations, but as soon as one dataset has 0 observations that fit the criteria, it seems that the macro spits out numbers from the dataset that was run previously. Here is my code below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro num (vorder, var);&lt;BR /&gt;&lt;BR /&gt;data final_&amp;amp;vorder;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; set t5;&lt;BR /&gt;&amp;nbsp;if &amp;amp;var ne .;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;proc summary data=final_&amp;amp;vorder completetypes chartype missing noprint;&lt;BR /&gt;&amp;nbsp;class outcome trt;&lt;BR /&gt;&amp;nbsp;types outcome*trt;&lt;BR /&gt;&amp;nbsp;output out=discrete;&lt;BR /&gt;&amp;nbsp;run;&lt;BR /&gt;&lt;BR /&gt;proc freq data=discrete;&lt;BR /&gt;&amp;nbsp;by _type_;&lt;BR /&gt;&amp;nbsp;tables outcome*trt/norow nopercent;&lt;BR /&gt;&amp;nbsp;weight _freq_/zeros;&lt;BR /&gt;&amp;nbsp;ods output crosstabfreqs_&amp;amp;vorder=crosstabfreqs_&amp;amp;vorder (where=(_type_2='01' or _type_2='11'));&lt;BR /&gt;&amp;nbsp;run;&lt;BR /&gt;&lt;BR /&gt;data CrossTabFreqs_&amp;amp;vorder /*(keep=vname rowlabel cell _level_ idlabel _type_)*/;&lt;BR /&gt;length vname $32 rowlabel $100 cell $32 idlabel $256;&lt;BR /&gt;set CrossTabFreqs;&lt;BR /&gt;vname =vnamex(scan(table,2,' '));&lt;BR /&gt;rowlabel=vvalueX(vname);&lt;BR /&gt;cell=cats(Frequency);&lt;BR /&gt;format colPercent 8.1;&lt;BR /&gt;set bign key=trt_dec/unique;&lt;BR /&gt;idlabel=cat(strip(vvalue(trt_dec)),' ^ ',cat('(N = ',bign,')'));&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to create another criteria that tells SAS to return nothing if there are no observations in the dataset? Thank you, any help is much appreciated!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;One easy solution to prevent reusing a data set is to delete it before the new version should be created.&lt;/P&gt;
&lt;P&gt;Assuming that the data set you may have issues with is the set DISCRETE as the output from Proc Summary then adding this somewhere before the proc summary step:&lt;/P&gt;
&lt;PRE&gt;proc datasets library=work;
delete discrete;
run;&lt;/PRE&gt;
&lt;P&gt;would prevent re-use. If the data set does not exist there is no error but you do get messages like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;NOTE: The file WORK.DISCRETE (memtype=DATA) was not found, but appears on a DELETE statement.

&lt;/PRE&gt;
&lt;P&gt;in the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code for checking 1) existence and 2) numbers of observations and handling the result is a bit longer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;More specific details about which sets to ignore would be helpful.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 22:29:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503584#M134600</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-11T22:29:43Z</dc:date>
    </item>
    <item>
      <title>Re: Macro issue when dataset has 0 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503598#M134605</link>
      <description>&lt;P&gt;See the example macros here, to extend your macro to appropriately handle the 0 case scenario.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most likely Example 13 or a combo of 8/9 depending on what exactly you're trying to do.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 23:42:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503598#M134605</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-11T23:42:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro issue when dataset has 0 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503647#M134626</link>
      <description>Instead of using the DATASETS procedure, I would recommend the resurrected DELETE procedure. That way, you do not get the library listing, and PROC DELETE is also faster, as it does not scan the whole library.</description>
      <pubDate>Fri, 12 Oct 2018 08:28:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503647#M134626</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-10-12T08:28:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro issue when dataset has 0 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503755#M134664</link>
      <description>&lt;P&gt;If I were to use this code from Example 13:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class="xis-code"&gt;data one;
x=1;
run;
data two;
stop;
run;

%macro drive(dsn);
%let dsid=%sysfunc(open(&amp;amp;dsn));
    
 %if &amp;amp;dsid ne 0 %then %do;
  %let cnt=%sysfunc(attrn(&amp;amp;dsid,nlobs));
  %let rc=%sysfunc(close(&amp;amp;dsid));
   %if &amp;amp;cnt ne 0 %then %do;
    proc print data=&amp;amp;dsn;
     title "This is data from data set &amp;amp;dsn"; 
    run;
   %end;
   %else %do;
    data _null_;
     title;
     file print;
     put _page_;
     put "Data set &amp;amp;dsn is empty.";
    run;
   %end;
 %end;
 %else %put &amp;amp;dsn cannot be open.;
   &lt;/PRE&gt;&lt;P&gt;How would i then transition into my own macro? I'm sorry, I'm new to using macros so am unfamiliar with some of the concepts&lt;/P&gt;</description>
      <pubDate>Fri, 12 Oct 2018 15:20:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503755#M134664</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2018-10-12T15:20:27Z</dc:date>
    </item>
    <item>
      <title>Re: Macro issue when dataset has 0 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503757#M134665</link>
      <description>&lt;P&gt;Would it work this way?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro num (vorder, var);&lt;BR /&gt;&lt;BR /&gt;data &amp;amp;dsn;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; set t5;&lt;BR /&gt;&amp;nbsp;if &amp;amp;var ne .;&lt;/P&gt;&lt;P&gt;output data=final_&amp;amp;vorder;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;proc summary data=final_&amp;amp;vorder completetypes chartype missing noprint;&lt;BR /&gt;&amp;nbsp;class outcome trt;&lt;BR /&gt;&amp;nbsp;types outcome*trt;&lt;BR /&gt;&amp;nbsp;output out=discrete;&lt;BR /&gt;&amp;nbsp;run;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Oct 2018 15:21:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503757#M134665</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2018-10-12T15:21:57Z</dc:date>
    </item>
    <item>
      <title>Re: Macro issue when dataset has 0 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503761#M134667</link>
      <description>You'd put your code in between the %else %do/%end section to allow you to have that code run. Read the detailed explanation of the macro below, that explains it step by step.</description>
      <pubDate>Fri, 12 Oct 2018 15:37:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503761#M134667</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-12T15:37:28Z</dc:date>
    </item>
    <item>
      <title>Re: Macro issue when dataset has 0 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503769#M134670</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/139341"&gt;@kmardinian&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I created this macro below and have been running it with datasets that contain observations, but as soon as one dataset has 0 observations that fit the criteria, it seems that the macro spits out numbers from the dataset that was run previously. Here is my code below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;....&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can test in the data step that is filtering the data and set a macro variable that you can use to control what code to run.&lt;/P&gt;
&lt;P&gt;I simple way is to use WHERE instead of IF and the END= option on the SET statement.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data final_&amp;amp;vorder;
  if eof then call symputx('nobs',_n_-1);
  set t5 end=eof;
  where &amp;amp;var ne .;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Make sure to put the CALL SYMPUTX() call BEFORE the SET statement. SAS data steps normally stop when they read past the end of the input data. So for an empty input set the statements after the SET statement never run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could then use the value of &amp;amp;NOBS to control your code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;nobs &amp;gt; 0 %then %do;
* code to process the data ;
%end;
%else %do;
* code to run when input set is empty ;
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 Oct 2018 16:00:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503769#M134670</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-10-12T16:00:08Z</dc:date>
    </item>
    <item>
      <title>Re: Macro issue when dataset has 0 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503780#M134675</link>
      <description>&lt;P&gt;Thank you so much! I appreciate all the help, this definitely help clarify a lot of things. It's working, I ended up using the NOBS option!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token macrostatement"&gt;%if&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;nobs &lt;SPAN class="token operator"&gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%then&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%do&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token comment"&gt;* code to process the data ;&lt;/SPAN&gt;
&lt;SPAN class="token macrostatement"&gt;%end&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token macrostatement"&gt;%else&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%do&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token comment"&gt;* code to run when input set is empty ;&lt;/SPAN&gt;
&lt;SPAN class="token macrostatement"&gt;%end&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;</description>
      <pubDate>Fri, 12 Oct 2018 16:17:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-issue-when-dataset-has-0-observations/m-p/503780#M134675</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2018-10-12T16:17:19Z</dc:date>
    </item>
  </channel>
</rss>

