title: SAS: Calling macro from within a Data step loop
body: <p>To refactor a program, I took a complex process I want to abstract and placed it within a macro.</p>
  
  <pre><code>%macro BlackBox();
    data _null_;
      put "This represents a complex process I want to abstract.";
    run;
  %mend;
  </code></pre>
  
  <p>The process needs to occur multiple times in succession, so the obvious solution is to place it within a loop.</p>
  
  <pre><code>data _null_;
    do i = 1 to 3;
      %BlackBox();
    end;
  run;
  </code></pre>
  
  <p>This, however, produces the following error.</p>
  
  <pre><code>ERROR 117-185: There was 1 unclosed DO block.
  </code></pre>
  
  <p>What is happening?  </p>
  
  <p>My best guess is that SAS is trying to run a data step within a data step.  </p>
  
  <p>I find that I can avoid this error by enclosing my loop within a macro and then immediately calling the macro.</p>
  
  <pre><code>%macro PerformDoLoop();
    %do i = 1 %to 3;
      %BlackBox();
    %end;
  %mend;
  %PerformDoLoop;
  </code></pre>
  
  <p>All this seems like an roundabout way of handling a fundamental programming task.  I'm hoping that learning more about why the data step approach fails will give me insight into how to complete this task more elegantly.</p>
  
  <p>Please understand that this is a simplified example used to illustrate the error I am encountering.  A real instance of the macro may take in arguments or return values.</p>