SAS Macros - Part 2 - Macro and Local Macro Variables

SAS Macros are as powerful as a dragon or an eagle, also they haunts SAS coders. Macros are used for automation purpose in a project; when you need to something repetitive, time and again, or your want to design a very complex algorithm in SAS, Macros are there to help you out.

I know these are little difficult to code, but if you imbibe the concept very well in a logical manner, believe me, you will find macros to be your best friend.

Dragons can also be "cute", you just need to control them.  Learn "How to train your dragon?"

The article has been written in continuation of  one of the previous articles covering Global Macro variables

If we look at the structure of macro programming, it looks like :

Let us now learn "Local Macro Variables and Macros" :

An example:

We have a data set :

Data Class;
infile datalines ;
Input Name $ Subject $ full_marks Marks_secured;
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

We need to create data of students individually.

We had seen how to do it using Global Macro variable (Click here to refer), now we are using macro to perform the task.

%Macro individual_data(name);

Data &name._Data;
set Class;
where name = "&name.";


So basically in the above code, we are creating a macro (individual_data), in which name is a "Local Macro Variable". 
With statement %individual_data(Aarya); we are calling the macro individual_data while assigning "Aarya" to name as value.

Do remeber to use macro variables with character value in " " only but not '  ' , as in single quotes macro variables are not resolved.

Now while we are clear about Global and Local macro variables and Macro, we should see few examples that would help us learn macro better. In the example, we would fuse local and macro variable in macro.

Click here for examples

Family of Macro related articles :

4.  Conditional execution in Macro

5.  Much more