Conditional Execution in Macro


<<< Click here to go back 


By conditional Execution, I mean :  Select and run the parts of code on the basis of certain logic. We use "If" "else" for logical operations in macros :

A SAS Macro with IF - ELSE condition looks like  :

%Macro Macro_name( a,b,c);

%IF expression %THEN %DO;

..................................
..................................
%END;

%ELSE %DO;

..................................
..................................
%END;

%Mend;

%Macro_name( 4,5,6);

Let's learn it by an example.


Consider the data set :

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;


Now we want to roll up this data at two levels : 
    1. Student Level
    2.  Subject Level.

It's just a matter of two PROC SQL queries, I know ... But still we would do it with Macro, just for the sake of learning.

Solution without Macro :

Proc SQL;
Create table student_wise as select name, sum(full_marks) as full_marks, sum(Marks_secured) as Marks_secured
from class
group by name
order by name;
Quit;

Proc SQL;
Create table Subject_wise as select Subject, sum(full_marks) as full_marks, sum(Marks_secured) as Marks_secured
from class
group by Subject
order by Subject;
Quit;

Let's now do it with a simple macro

%Macro Simple (Report_type);

Proc SQL ;
Create table &Report_type._wise as
Select &Report_type., sum(Marks_secured) as sum_secured, sum(full_marks) as sum_total
from class 
group by &Report_type.
order by &Report_type.;
Quit;

%Mend ;

%Simple (Subject);
%Simple (Name);


Let's now use a macro with "IF"s


%macro understand_if_then(Report_type);

%if &report_type. = Name %then %do ;

Proc SQL ;
create table student_wise as
select name, sum(Marks_secured) as sum_secured, sum(full_marks) as sum_total
from class 
group by name
order by name;
Quit;
%end ;

%else %if &Report_type. = Subject %then %do ;

Proc SQL ;
create table subject_wise as
select Subject, sum(Marks_secured) as sum_secured, sum(full_marks) as sum_total
from class 
group by Subject
order by Subject;
Quit;
%end ;

%Mend ;

%understand_if_then(Subject);
%understand_if_then(Name);


How the above code works ?






The above execution can be checked in SAS log, with enabler options MPRINT MLogic;

To know more about these options read >>> Useful options and tips whiles writing SAS Macros

:: Let us learn one more method for conditional execution ::



Click here to go next >>>



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.