Call Execute


<<< Click here to go back


Let's first learn Call Execute with an example :

We are using same data as we have used across macro articles :

Data Class;
infile datalines ;
Input Name $ Subject $ full_marks Marks_secured;
cards;
Rajat Maths 100 52
Vinod Maths 100 64
Rakesh Maths 100 57
Geeta Maths 100 58
Bharat Maths 100 86
Rajesh Maths 100 81
Monal Maths 100 67
Aarya Maths 100 88
Ritesh Maths 100 68
Neha Maths 100 88
Rajat English 150 53
Vinod English 150 69
Rakesh English 150 52
Geeta English 150 59
Bharat English 150 88
Rajesh English 150 59
Monal English 150 71
Aarya English 150 66
Ritesh English 150 68
Neha English 150 77
;
Run;

We need to break the data student wise and make separate datasets individually. If you have gone through our previous articles on macro and you memory serves you well, we have done the task with another method (%do loop) in macro itself. Click here to check the same. 

Now we would perform the task with "Call Execute" !

First let us make a list of names.

Proc sql;
Create table name_list as select distinct name from class;
quit;

We write a macro :

%Macro subset (name);

Data &name._data;
set  Class;
where name = "&name.";
Run;

%Mend ;

Now we would execute macro with call execute.

DATA _NULL_;
SET name_list ;
CALL EXECUTE('%subset(name = ' || name || ');');
RUN;


... and we get name wise data in the library.


Basically we are calling the "subset" macro in a datastep and passing the value observation wise to macro variable "name". It is quite handy method to call a macro in repetitive manner. Now I believe you understand the relevance of term "Call Execute", it calls to execute !




Last topic in SAS macro (that we are covering)

Calling one macro from another macro


We will take up the same example one more time to understand various other ways of calling a macro.

1.  Calling one macro from other within active Program Editor Windows.

2.  Calling one macro from other inactive Program Editor Windows ( Can also call a macro saved in another .sas program file).

On the class data, let's run the following program :

Proc sort data = class; by subject name; run;
Data class;
set class;
count +1 ;
by subject ;
if first.subject then count = 1;
run;

The code, as we have seen in previous examples too, would give each student a unique counter.

Now with the help of this counter we can break the data student wise. We have earlier broken down the data, student wise several ways in the previous article on macro series.

This time, we not only want to break the data, but also want to process the student wise part data.

For processing part data, we write a macro :

%Macro summary(data_name);
Proc summary data  = &data_name.;
var marks_secured;
output out = summary_&data_name.
sum = ;
Run;
%Mend;

For breaking data main data into parts, we write a Second macro. But during this macro itself, we call the first one.

%Macro Break_the_data;

%do i = 1 %to 5;

Data part_&i.;
set class;
Where count = &i.;
run;

%summary(part_&i.)

%End;

%Mend;
%Break_the_data;

That's how we can call one macro from another.

Now imagine, we store the firs macro i.e. Macro summary in a seperate sas code (.sas file), and want to use it the same way as we have used above. 

No worries ! even without opening the code, you can use that macro in the current SAS session.

%Include Magic



With this we are concluding macro chapter for now. May be in future, we come up with few more articles with examples on macros.


Enjoy reading our other articles and stay tuned with ...

Kindly do provide your feedback in the 'Comments' Section and share as much as possible. 


No comments:

Post a Comment

Do provide us your feedback, it would help us serve your better.