CALL EXECUTE In SAS (With 10+ Examples)

The SAS macro language is an extremely flexible tool which facilitates SAS programmers with the ability to re-run the same code for a set of different parameters.

It can be done using DO-LOOP by passing required parameters but alternatively you could also use the CALL EXECUTE routine which is more robust and efficient. 

Call Execute can be used with a data step when you want to create a new output dataset. Whereas “data _null_” will be used when you want to execute a call execute routine without creating an output dataset within the data step. In most of the cases you’ll be using Call Execute with data _null_ statement.

Syntax:

				/* basic Syntax */

data _null_;
	call execute('argument');
run;
			

The CALL EXECUTE routine accepts a single argument that is a character string or character expression.

The character expression is usually a concatenation of strings containing SAS code elements to be executed after they have been resolved.

CALL EXECUTE – A Simple Example

A simple example could be printing text messages in the logs using the %put function using call execute routine. Since we don’t want to create an output dataset, we will be using data _null_ statement. 

This example shows how to print a sample text message “Hello world – LearnSASCode.com” into the sas log.

				/* print text msg into sas log */

data _null_;
	call execute('%put Hello world - LearnSASCode.com;');
run;
			
Call execute in sas to print into log

Example 2: Create A New Dataset With Call Execute

You can literally write entire SAS code line by line enclosed in the multiple call execute statements by passing each line as an argument.

In this example we will execute the data step to create a new dataset work.class_ds using existing dataset sashelp.class

				/* call execute to run data step code */

data _null_;
   call execute("data class_ds;");
   call execute("set sashelp.class;");
   call execute("run;");
run;
			
Call Execute to run SAS code

Example 3: How To Print Dataset Using Call Execute

In the previous example you have created a new dataset work.class_ds using call execute statement. Let’s print the original dataset sashelp.class observations using call execute routine.

PROC PRINT procedure is getting executed here but through the call execute routine. The below code prints the first five sample observations from the sashelp.class dataset using obs=option.

				/* print dataset using call execute */

data _null_;
   call execute("proc print data=sashelp.class(obs=5);");
   call execute("run;");
run;
			
Print dataset using call execute in SAS

Example 4: CALL EXECUTE With Including Values From A SAS DATASET

The call execute becomes even more powerful when it is used with the SET statement to read values from the input SAS dataset in the arguments.

Call execute arguments can be string or character expressions. The char expression can be written with pipe concatenation operator or CATs function to concatenate the strings to form the argument.

The following example will print the list of first 10 sample student names from sashelp.class dataset using call execute and pipe concatenation operator.

				/* call execute with pipe concatenation operator */

data _null_;
	set sashelp.class(obs=10);
	call execute('%put Student Name is :'||strip(name)||'; ');
run;
			
Call execute with argument string expression

You can rewrite the above code using CATS function instead of pipe concatenation operator. This is my favourite way of writing arguments in call execute routine. 

Here is now you can rewrite the above code with cats function.

				/* call execute with CATS function*/

data _null_;
	set sashelp.class(obs=10);
	call execute(cats('%put Student Name is :',strip(name),'; '));
run;
			
Call EXECUTE With CATS function in SAS

Example 5: Create Multiple Datasets Using CALL EXECUTE

This example is the perfect example of how you can use dataset values to pass it as arguments in the call execute routine.

In the below example five new datasets will be created and named as *student-name*_ds. There is another call execute routine used to run the proc print code to print dataset observations.

				/* Create multiple datasets using call execute */

data _null_;
 	set sashelp.class(obs=5);
	call execute(cats('data work.', strip(name), '_ds ;', 
 			'set sashelp.class ;', 
 			'where name= "', strip(name), '";',
 			'run;'));
 			
 	call execute(cats('proc print data=work.', strip(name), '_ds ;',
 				'title ','" Data set: work.', strip(name), '_ds" ;',
    		'run;'));
run;
			
Create multiple datasets using call execute in SAS

Example 6: Call SAS Macro Using CALL EXECUTE Routine

You can use call execute routine to invoke sas macros with arguments. Here is the basic syntax of how to call SAS macro through call execute.

				/* basic syntax */

data _null_;
	call execute('%macro_name();');
run;

			

Here is the simple SAS macro %print_ds() that prints a given dataset (“sashelp.class”). This macro will be called through the call routine in data _null_ statement.

				/* sas macro to print sashelp.class dataset */
%macro print_ds();

	/* print dataset */
	proc print data=sashelp.class(obs=10);
		title "Data set : sashelp.class " ;
	run;

%mend print_ds;

/* call sas macro through call execute */
data _null_;
	call execute('%print_ds() ;');
run;
			
SAS macro to print dataset values using call execute

Example 7: Call SAS Macro To Create Multiple Datasets Using CALL EXECUTE

 

In this example a new SAS macro %create_ds(name=) will be defined that will take names as an input parameter and creates three new datasets named *student-name*_ds and the same dataset will be printed in the output. 

The student names will be passed as a parameter from dataset values through call execute routine.

				/* sas macro to create multiple datasets */
%macro create_ds(name=);

	/* create dataset */
	data work.&name._ds;
		set sashelp.class;
		where name="&name.";
	run;

	/* view dataset */
	proc print data=work.&name._ds;
		title "Data set : work.&name._ds " ;
	run;

%mend create_ds;

/* call sas macro with student names as a parameter */
data _null_;
	set sashelp.class(obs=3);
	call execute(cats('%create_ds(name=', strip(name), ') ;'));
run;
			
SAS Macro To Create multiple datasets using CALL EXECUTE

Example 8: Create Macro Variables Through CALL EXECUTE

You can create macro variables using PROC SQL with INTO: statement. If you try to do the same with the SAS macro calling through call execute routine, it will give you some error for the first run. 

Let’s deep dive into this specific case. The following example defines the SAS macro %WriteNameAge() and invokes this macro through the call execute routine. 

To simplify this example we have added a fiter, name=”Barbara” to extract only one row from sashelp.class, and create and assign values to the macro variables student_name and student_age.

				/* Create Macro Variables Through CALL EXECUTE */
%macro WriteNameAge();
	proc sql;
		select name, age into:student_name,:student_age from sashelp.class 
		where name="Barbara";
	quit;
%put "My name is &student_name and I am &student_age years old";

%mend WriteNameAge;


/* call macro using call execute in sas*/
data _null_;
	call execute('%WriteNameAge();'); 
run;

			
Warning and Error in SAS Macro due to CALL EXECUTE statement

You will get the above error when you run this macro for the first time. You can solve this issue by using %nrstr in the call execute argument. 

Solution: Here is the revised code to call SAS macro using call execute routine.

				/* call sas macro with %nrstr */

data _null_;
	call execute('%nrstr(%WriteNameAge();)');
run;
			
SAS macro variable through Call Execute with %nrstr

FAQ – CALL EXECUTE In SAS

What is CALL EXECUTE in SAS?

CALL EXECUTE is a SAS statement that enables you to generate and execute SAS code dynamically during DATA step execution.

How do I use CALL EXECUTE in SAS?

To use CALL EXECUTE in SAS, you need to specify the SAS code to be executed as a character string and pass it to CALL EXECUTE.

 

You can also use various options such as the SYSEVALF function and the SYSEXEC statement to execute system commands.

What are the advantages of using CALL EXECUTE?

CALL EXECUTE provides a flexible and powerful way to generate and execute SAS code dynamically.

 

It allows you to create custom programs that can be tailored to your specific needs and can be easily updated as your data changes

How do I resolve macro calls or functions in CALL EXECUTE?

To resolve macro calls or functions in CALL EXECUTE, you need to use the %STR or %NRSTR function to mask the macro call or function.

 

This ensures that the macro call or function is resolved at the appropriate time during execution.