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