title: conditionally execute macro function
body: <p>I want to conditionally execute macro function dependent on existence of table</p>
  
  <p>Data</p>
  
  <pre><code>data have;
      do i = 1 to 5;
          output;
      end;
  run;
  </code></pre>
  
  <p>Table I want to condition on</p>
  
  <pre><code>data counttbl;
      infile datalines delimiter='::'; 
      format variable $char400. condition $char400.;
      input variable $ condition $;
      datalines;
  runcount::i&gt;1
  ;
  run;
  </code></pre>
  
  <p>Some tests to show that I can condition on <code>counttbl</code> existence (works as expected)</p>
  
  <pre><code>data _null_;
      call execute("data want;");
      call execute("set have;");
      if exist("work.counttbl") then
          call execute("tmp = 1;");
      call execute('run;');
  stop;
  run;
  </code></pre>
  
  <p>The above creates column <code>tmp = 1</code></p>
  
  <pre><code>proc delete data=work.counttbl;
  run;
  
  data _null_;
      call execute("data want;");
      call execute("set have;");
      if exist("work.counttbl") then
          call execute("tmp = 1;");
      call execute('run;');
  stop;
  run;
  </code></pre>
  
  <p>After deleting the table, the above does not create the column <code>tmp</code></p>
  
  <p>Macro function to execute</p>
  
  <pre><code>%macro apply_change();
      call execute('if _n_ eq 1 then do;');
      do until (eof);
          set counttbl (keep=variable) end=eof;
          call execute(strip(variable) || ' = 0;');
      end;
      call execute('end;');
      call missing(eof);
      do until (eof);
          set counttbl end=eof;
          call execute(strip(variable) || ' + (' || strip(condition) || ');');
      end;
      call missing(eof);
  %mend apply_change;
  </code></pre>
  
  <p>Works fine when <code>counttbl</code> exists</p>
  
  <pre><code>data _null_;
      call execute("data want;");
      call execute("set have;");
      if exist("work.counttbl") then
          %apply_change()
      call execute('run;');
  stop;
  run;
  </code></pre>
  
  <p>Throws error when <code>counttbl</code> is deleted - I want it to simply skip executing the macro function</p>
  
  <pre><code>proc delete data=work.counttbl;
  run;
  
  data _null_;
      call execute("data want;");
      call execute("set have;");
      if exist("work.counttbl") then
          %apply_change()
      call execute('run;');
  stop;
  run;
  
  ERROR: File WORK.COUNTTBL.DATA does not exist.
  </code></pre>
  
  <p>Thanks in advance for your help</p>