BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PGStats
Opal | Level 21

Is there a functional difference between these two code blocks?

 

filename execute temp;
data _null_;
file execute; put "data test;";
do i = 1 to 5;
    file execute; put "x=" i "; output;";
    end;
file execute; put "run;";
run;
%include execute / source2;

data _null_;
call execute("data test;");
do i = 1 to 5;
    call execute(cats("x=", i, "; output;"));
    end;
call execute("run;");
run;

They seem perfectly equivalent to me. When would the two programming approaches differ?

PG
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

They're the same (at least they appear to be) for this particular example, but there can be differences in theory.  Any macro language statements (%let, macro invocations, etc.) execute at different times with these two approaches.  Macro language statements execute immediately when initiated by CALL EXECUTE, which is not necessarily the order you would expect.  Only the SAS language statements generated by CALL EXECUTE wait for the current DATA step to complete.  On the other hand, %INCLUDE executes the statements in the order you would expect.

 

A simple example:

 

data _null_;

call execute ('%let color=Blue; data test; color=symget("color"); run; %let color=Red;');

run;

 

 

View solution in original post

7 REPLIES 7
Astounding
PROC Star

They're the same (at least they appear to be) for this particular example, but there can be differences in theory.  Any macro language statements (%let, macro invocations, etc.) execute at different times with these two approaches.  Macro language statements execute immediately when initiated by CALL EXECUTE, which is not necessarily the order you would expect.  Only the SAS language statements generated by CALL EXECUTE wait for the current DATA step to complete.  On the other hand, %INCLUDE executes the statements in the order you would expect.

 

A simple example:

 

data _null_;

call execute ('%let color=Blue; data test; color=symget("color"); run; %let color=Red;');

run;

 

 

PGStats
Opal | Level 21

Thanks! I tried

 

%let color=yellow;
data _null_;
call execute ('%let color=Blue; data test1; color=symget("color"); run; %let color=Red;');
color=symget("color");
put "Call Execute Color =" color;
run;

filename execute temp;
%let color=yellow;
data _null_;
file execute; put '%let color=Blue; data test2; color=symget("color"); run; %let color=Red;';
color=symget("color");
putlog "Include Temp File Color =" color;
run;
%include execute / source2;

I think I understand a bit better the difference now. As you said, what's going on is not immediately obvious. Would it be fair advice to keep call execute for the simpler tasks where the timing of macro execution is not an issue?

PG
Rick_SAS
SAS Super FREQ

My two cents: Writing the whole file and the using %INCLUDE is easier to understand and debug.  It works exactly like the corresponding SAS program would, and the interaction between global statements, macro, DATA step compilation, and DATA step execution is easier to figure out. As an added bonus, you can look at the program and even modify it if necessary.  You can also run the program many times without regenerating it.

 

The fact that certain statements are executed immediately whereas others are compiled and then executed at runtime makes this a hard problem.

 

In an interactive procedure (DATASETS, SQL, GLM, IML, ...) the problems with combining macro and CALL EXECUTE can be even more complex. It can require understanding the way that SAS compiles and executes statements, as shown in this PROC IML example that updates a macro variable in a loop.  See also this example of using CALL EXECUTE to set a global statement.

Ksharp
Super User

The implied difference is %include will execute the code directly ,will not interpret it .

While the code generated by  call execute() will be interpreted by compiler and then execute it .

Therefore %include is going to be faster .

 

For the small code , they are almost the same. I like CALL EXECUTE() better, but Tom like %include better though.

CatCol
Fluorite | Level 6
I think in both cases %include or call execute the code has to be interpreted and then executed. I see no difference. Am I missing something?
Ksharp
Super User

No. %include is not going to be interpreted, while call execute() will . Did you see my example at last ? That is reason explain why the macro variable is not resolved in LOG , but %include will not  complain that .

 

the code generated by call execute() will firstly pass through compiler and try to resolve macro value, if can't resolve , it will print a WARNING message. 

the code generated by %include  will not pass through compiler, just execute the code directly like it is in code editor .and you will not see any WARNING message. 

Ksharp
Super User

One more different is the order of macro variable resolved is different .

Note: start two new sas session to run these two code . and You will see the difference.

 

/* First Code*/

filename execute temp;
data _null_;
file execute;
i=1;
put 'data _null_;call symputx("x",' i ');run; %put &x;';
run;
%include execute / source2;

 

 

 

/*Second Code*/

data _null_;
i=1;
call execute(cats('data _null_;call symputx("x",' ,i, ');run; %put &x;'));
run;

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
  • 7 replies
  • 1958 views
  • 5 likes
  • 5 in conversation