DATA Step, Macro, Functions and more

Including a program within itself

Posts: 24

Including a program within itself


I'm trying to accomplish the following.

I have a program, which I want to call itself once. So if the name of my program is, then I figure I need something in lines of the following:


<bulk of program>

do this once:

%include "";


Through the first run of the program, it should run like it uses to, but through the second run, I want to change a parameter. I imagine that the bulk of the program should contain code similar to:

%if &ok %then %do;

change parameters;


So I suppose that I would also like the variable &ok to be set to 1, when the program is run for the second time, and before that the parameter should be 0.

For some reason, my own attempts ended up in infinite loops.

Kind regards,

Rasmus Hedegaard.

Super User
Posts: 17,823

Re: Including a program within itself

Wrap it in macro code instead.

Assuming you save the code in another sas program as

%macro my_program (age=my_value);

***your code***;

proc print data=sashelp.class;

where age=&my_value;



%include; *if its in a separate program file;



Posts: 7,363

Re: Including a program within itself

: The forum could probably provide a better answer if you describe what you are trying to accomplish in less general terms.  That said, I agree with Fareeza, but will guess that you want to include the condition in a %do %while loop in the macro.  You just have to insure that you have defined the condition such that it won't run infinitely if the condition is never met.  e.g., you can make it an or condition like if some criterion is reached or you have exceeded X iterations.

Regular Contributor
Posts: 241

Re: Including a program within itself

%include is not a macro invocation, but rather something like a "compiler directive" in other computer languages. This affects the timing when exactly the source is "included," which is earlier than the timing expected by a macro invocation.

Anyway, I agree with Arthur in that there may be a better way to implement what you are trying to do. But, if you insist, then here is something close to what you asked for.

  /* save this file as c:\temp\ before you submit it */
  x cd "c:\temp\";
  %macro bulk(ok);
    %put this is the <bulk> &=ok;
  %mend  bulk;
  data _null_;
    if not symexist("ok") then do;
      call execute('%global ok; %let ok=0;');
      call execute('%bulk(ok=&ok);');
      call execute('%let ok=1;');
      call execute('%include "c:\temp\";');
    end; else do;
      call execute('%bulk(ok=&ok)');
      call execute('%symdel ok;');
  /* on log -- some notes removed
  this is the <bulk> OK=0
  this is the <bulk> OK=1

Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation