BookmarkSubscribeRSS Feed
Hedegaard
Calcite | Level 5

Hello,

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 test.sas, then I figure I need something in lines of the following:

_________________________

<bulk of program>

do this once:

%include "test.sas";

_________________________

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;

%end;

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.

3 REPLIES 3
Reeza
Super User

Wrap it in macro code instead.

Assuming you save the code in another sas program as my_program.sas

%macro my_program (age=my_value);

***your code***;

proc print data=sashelp.class;

where age=&my_value;

run;

%mend;

%include my_program.sas; *if its in a separate program file;

%my_program(age=14);

%my_program(age=15);

art297
Opal | Level 21

: 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.

chang_y_chung_hotmail_com
Obsidian | Level 7

%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\test.sas 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\test.sas";');
    end; else do;
      call execute('%bulk(ok=&ok)');
      call execute('%symdel ok;');
    end;
  run;
  /* on log -- some notes removed
  this is the <bulk> OK=0
  ...
  this is the <bulk> OK=1
  */
 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 935 views
  • 0 likes
  • 4 in conversation