SAS : Call Execute Made Easy

This tutorial explains how to use CALL EXECUTE in SAS and its benefits, along with examples.

What is CALL EXECUTE in SAS?

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.

Use cases for CALL EXECUTE
  1. 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.
  2. 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.
  3. 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.
Example 1

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 sets
Data 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

  1. If x is less than or equal to 30, then Groups = 3;
  2. If x is less than or equal to 20, then Groups = 2;
  3. 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.
SAS : Call Execute
Log : CALL EXECUTE

Let's create a new character variable using the following conditions :

  1. If x is less than or equal to 30, then Class = "C";
  2. If x is less than or equal to 20, then Class = "B";
  3. 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_ ;
input mydata $char50. ;
call execute
("proc print" || " data = " || strip(mydata) || ";" || "run ;");
cards;
temp
temp2
output
run;
The above program prints 3 datasets - temp temp2 output.

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_;
call execute ('%mymacro(6)');run;
 Example 4 : Dynamically Call Macro
%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; 
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

7 Responses to "SAS : Call Execute Made Easy"
  1. 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.

    ReplyDelete
    Replies
    1. Thank you for your kind words. Wish you all the best for your Base SAS certification exam.

      Delete
  2. what is the purpose for the call execute?

    ReplyDelete
  3. I 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.

    ReplyDelete
  4. Congratulations, and please keep on going. Your blog is really usefull

    ReplyDelete
  5. Congratulations, and please keep on going. Your blog is really usefull

    ReplyDelete
  6. What is the use of || in example 2 ie "proc print" || " data
    Can't we use proc print data=

    ReplyDelete

Next → ← Prev