BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
Hello All,
Can you please explain how call execute works?I tried it in my code after reading some literature on it but couldnt achieve what is desired.I would rather learn about it first then use it.
Thanks for you time and help
SASPHILE
8 REPLIES 8
LinusH
Tourmaline | Level 20
As you said, there's a lot of literature, on-line doc, samples etc out there. In to explain to you, we'll need to know kind of problem you have, what is it you want to achieve?

/Linus
Data never sleeps
SASPhile
Quartz | Level 8
Linus,
Thanks for the response. In general I want to know the functionality of it thats all.
SASPHILE
Cynthia_sas
SAS Super FREQ
Hi:
Imagine that you're going through a stack of file cards. Standing next to you is your assistant. Every time you come to a card where there is an 'XXX' in the upper right hand corner of the card, you write "RUN XXX Program" on the card and hand the card to your assistant. If the card has 'YYY' in the upper right hand corner, you write "RUN YYY Program" on the card and hand the card to your assistant. If the card has anything else on it, you don't write anything and you don't hand the card to your assistant. The assistant stands there while you go through the WHOLE stack of cards. The assistant doesn't do ANYTHING -at all- until you are all done looking at the cards.

Then, when you are done, completely done, with the whole stack of cards, the Assistant takes over, goes into another room and runs each program, as you wrote it down.

The reason for using CALL EXECUTE is because you want to do -something- for every observation or for selected observations in your data (the stack of cards). (For example, you may want to execute a special Macro program using every observation in your data to provide some value, OR, you may want to execute some program when VAR1='XXX' or VAR1='YYY' in your data.)

The CALL EXECUTE facility allows you to place instructions, either a SAS program or a macro program call, into a stack (like the cards you handed to your assistant up above). NOTHING happens until the data step that builds the CALL EXECUTE statements has finished executing.

So, for example, if you used this program:
[pre]
data _null_;
length task1 task2 $150;
set sashelp.class;
if sex='F' and name in ('Barbara', 'Mary') then do;
task1 = 'proc print data=sashelp.class; title "'||trim(name)||'"; where name ="'||trim(name)||'"; run;';
call execute(task1);
end;
else if sex = 'M' and name in ('Alfred', 'William') then do;
task2 = 'proc freq data=sashelp.class; title "Just Some Program";';
if name = 'Alfred' then task2 = trim(task2)||' tables height; run;';
else if name = 'William' then task2 = trim(task2)||' tables age; run;';
call execute(task2);
end;
run;
[/pre]

The data step program is creating a character variable, either TASK1 or TASK2 based on certain conditional criteria. The character variable is either holding the statements for a PROC PRINT program or a PROC FREQ program. The PROC PRINT program uses the value of the NAME variable when it builds TASK1. The TASK2 variable just holds code for PROC FREQ.

The entire SASHELP.CLASS data set must be read first (going through the stack of cards). As the program goes through the dataset, it is handing instructions (what's inside the CALL EXECUTE statement) to an assistant -- the "stack" or queue that will start to execute as soon as the data step is finished. The partial log from the above program is shown below:
[pre]
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


NOTE: CALL EXECUTE generated line.
1 + proc freq data=sashelp.class; title "Just Some Program"; tables height; run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE FREQ used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


2 + proc print data=sashelp.class; title "Barbara"; where name ="Barbara"; run;

NOTE: There were 1 observations read from the data set SASHELP.CLASS.
WHERE name='Barbara';
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


3 + proc print data=sashelp.class; title "Mary"; where name ="Mary"; run;

NOTE: There were 1 observations read from the data set SASHELP.CLASS.
WHERE name='Mary';
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


4 + proc freq data=sashelp.class; title "Just Some Program"; tables age; run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE FREQ used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds

[/pre]

Processing is a little more complicated than just a stack or queue. My program shows straightforward SAS code constructed inside the program.
Generally, people don't do this, but it is a useful place to start to understand how CALL EXECUTE works. Usually, people invoke a SAS macro
program with a CALL EXECUTE statement -- that means before what's in the stack can be executed, it must be passed to the macro processor
for macro program and variable resolution.

I have always found the examples in the documentation to be good:
http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/a000543697.htm
http://support.sas.com/kb/24/634.html

You could also search for papers on this topic. And, Art Carpenter's and Michele Burlew's books on SAS Macro topics are also good reference tools.

cynthia
SASPhile
Quartz | Level 8
Thanks Cynthia,
I thought call execute is used for macro invocation purposes.Thanks for demonstrating this example.
SASPhile
Quartz | Level 8
For the following code:
SYNTAX TO PASS PARAMETERS is shown in data _null_step right?
data dates;
input date $;
datalines;
10nov97
11nov97
12nov97
;

data reptdata;
input date $ var1 var2;
datalines;
10nov97 25 10
10nov97 50 11
11nov97 23 10
11nov97 30 29
12nov97 33 44
12nov97 75 86
;

%macro rept(dat,a,dsn);
proc chart data=&dsn;
title "Chart for &dat";
where(date="&dat");
vbar &a;
run;
%mend rept;

data _null_;
set dates;
call execute('%rept('||date||','||'var1,reptdata)'); ------SYNTAX TO PASS PARAMETERS?
run;
Cynthia_sas
SAS Super FREQ
Hi:
I prefer to use keyword parameters for my macro programs (instead of positional parameters), and, I rarely use dates that are character variables but otherwise, this looks correct. Does the code give you the desired result??? I'd probably use PROC GCHART instead of PROC CHART and if you used GCHART, you would definitely need a QUIT statement for PROC GCHART.

cynthia
SASPhile
Quartz | Level 8
I tried on small piece of code.I will try in the main program and let you know,
SASPHILE.
Cynthia_sas
SAS Super FREQ
Hi:
About this statement in your previous post:
I thought call execute is used for macro invocation purposes.

According to the doc CALL EXECUTE(argument): "Resolves the argument, and issues the resolved value for execution at the next step boundary"

While the argument CAN be an invocation of a macro program, it doesn't HAVE to be the invocation of a macro program. It can be any syntactically correct string that you want to have executed after the next step boundary. As you saw in my example, it could be any syntactically correct SAS code that is valid to execute.

Of course, since a SAS macro program is one way to package SAS code, invocation of a macro program as the CALL EXECUTE argument is what people USUALLY do, but it's not the ONLY thing they can do.

According to the doc, the argument can be:
--a character string, enclosed in quotation marks.
--the name of a DATA step character variable whose value is a text expression or a SAS statement to be generated.
--a character expression that is resolved by the DATA step to a macro text expression or a SAS statement.

I'm a fan of the data step character variable method because I can check to see whether my string is syntactically correct -before- coding the CALL EXECUTE statement.

cynthia

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
  • 8 replies
  • 1095 views
  • 0 likes
  • 3 in conversation