This tutorial explains how to use CALL EXECUTE in SAS and its benefits, along with examples.
The purpose of the CALL EXECUTE routine in SAS is to generate and execute SAS code dynamically during the execution of a DATA step. It allows you to create and execute SAS statements or steps based on the values of variables or other dynamic factors.
The CALL EXECUTE routine is often used in situations where you need to generate SAS code based on some criteria or data conditions that are only known during the runtime of the program. It provides a way to generate and execute SAS code dynamically rather than writing static code.
- Creating dynamic macro calls: You can use CALL EXECUTE to generate and execute macro calls with varying arguments or parameters based on the values of variables.
- Generating conditional data processing steps: You can use CALL EXECUTE to conditionally create and execute DATA steps based on specific data conditions or variable values.
- Dynamic creation of SAS code: You can use CALL EXECUTE to generate and execute SAS statements or data manipulation operations based on the values of variables or other runtime factors.
Suppose you have two data sets named "temp" and "temp2". You are asked to form a group based on the logical conditions given in the other dataset "temp2" and apply the conditions in dataset temp.
Create sample data setsData temp;
input x;
cards;
5
10
15
20
25
30
;
run;
Data temp2;
input var $ Score rank Section $;
cards;
x 30 3 C
x 20 2 B
x 10 1 A
;
run;
Use columns VAR, SCORE and RANK of dataset TEMP2 to create the following conditions and apply them in dataset TEMP
- If x is less than or equal to 30, then Groups = 3;
- If x is less than or equal to 20, then Groups = 2;
- If x is less than or equal to 10, then Groups = 1;
Solution : CALL EXECUTE
Data _null_; set temp2 end=last; if _n_=1 then call execute ('Data output; set temp;'); call execute ("if " ||strip(Var)|| " LE " ||strip(Score) || " then " || "Groups" || "=" ||strip(rank) ||";"); if last then call execute (' run;'); run;Note - All static components should in single or double quotes and variable components should not be in quotes.
See the code processed in the LOG window as shown in the image below.
Log : CALL EXECUTE |
Let's create a new character variable using the following conditions :
- If x is less than or equal to 30, then Class = "C";
- If x is less than or equal to 20, then Class = "B";
- If x is less than or equal to 10, then Class = "A";
Solution : CALL EXECUTE
Data _null_; set temp2 end=last; if _n_=1 then call execute ('Data output; set temp; length Class $10.;'); call execute ("if " ||strip(Var)|| " LE " ||strip(Score) || " then " || "Class" || "=" || """" || strip(Section) ||""""|| ";"); if last then call execute (' run;'); run;Example 2 : How to print Multiple Datasets
data _null_ ;The above program prints 3 datasets - temp temp2 output.
input mydata $char50. ;
call execute
("proc print" || " data = " || strip(mydata) || ";" || "run ;");
cards;
temp
temp2
output
run;
Example 3 : Call a Macro
%macro mymacro(k);
data want;
set temp;
%do i = 1 %to &k;
if _N_ = &i then y = %eval(&i.* 10);
%end;
run;
%mend;
data _null_;Example 4 : Dynamically Call Macro
call execute ('%mymacro(6)');run;
%macro mymacro(i,j);
%put first = &i. second = &j.;
%mend;
DATA example;
input x y $32. ;
datalines;
1 temp
2 temp2
;
run;
data _null_;
set example;
call execute('%MyMacro('||x||','||y||')');
run;
Fantastic tutorials and blog. I am preparing for Base SAS certification exam so I am learning different topics from your blog. I was exhausted when I found. This is very completed and amazing.
ReplyDeleteThank you for your kind words. Wish you all the best for your Base SAS certification exam.
Deletewhat is the purpose for the call execute?
ReplyDeleteI was thinking of making video tutorials with simple examples. Your blog is really great in the reading format. Simple examples are the key to quickly understand concepts. Good luck. Thank you.
ReplyDeleteCongratulations, and please keep on going. Your blog is really usefull
ReplyDeleteCongratulations, and please keep on going. Your blog is really usefull
ReplyDeleteWhat is the use of || in example 2 ie "proc print" || " data
ReplyDeleteCan't we use proc print data=