Thursday, April 27, 2017

Macro Basics (SAS)


Macros are considered an “Advanced” programming concept, as they are not mentioned at all in the Base SAS Certification Prep Guide. Regardless of this conception, SAS Macros are not difficult to utilize. The greatest difficulty that will be encountered by the user, if the user has a prior programming background, is that SAS Macros do not conform to typical programming paradigms.

To create a macro (function), you will begin the line of code:

%MACRO <MACRONAME>;

For our example, let’s use the name, “DATABLOG”.

So far, our macro would resemble the following:

%MACRO DATABLOG;

Next, we have to make the macro do something.

This is probably a good time to introduce %DO;

%DO, as it exists within a macro, functions similarly to the way in which functions outside of a macro. As a programmer, you will be using %DO to animate most of your macro functionality. Therefore it is fair to say that %DO, does something within your macro.

Let’s make our macro contain a %DO loop that iterates 5 times.

%MACRO DATABLOG;

%DO I = 1 %to 5;


The “I” counter variable which the %DO loop utilizes, can be referred back to as a variable within the %MACRO.

Referring back to variables in SAS which are assigned by the user in a SAS program is notated with a:

&<VARIABLENAME>;

So, if we were to refer to our “I” variable, the code would resemble:

&I;

To assign values to variables within a macro to a variable, you would use %LET.

In the case of the counter variable, %LET is un-necessary element, as the assignment is made within the %DO loop. However, for the sake our example, let’s say that we wanted to create a new variable which has its value derived from the “I” variable.

%let y = &I + 1;


While creating this variable in such a manner will return no errors, the way in which it is defined will return the value:

“<valueofI> + 1”;

Instead of the sum value of “I” + 1.

To fix this, you need to add %eval function.

%eval tells SAS to evaluate the data within the functional parameters.

So:

%let y = %eval (&I + 1);

Will provide SAS with the sum value of I+1. Let us add it to our macro.

%MACRO DATABLOG;

%DO I = 1 %to 5;

%let y = %eval (&I + 1);

So now, what function do we want our macro to perform?

For the sake of our example, let’s say that you want to compare values across 6 adjacent variable columns which contain numerical values. If the first a value within a column is equal to an adjacent column within any row, a new variable “Value1Flag”, is given the value of “1”.

%MACRO DATABLOG;

%DO I = 1 %to 5;

%let y = %eval (&I + 1);


If num&I = num&y then VALUE1FLAG = 1;


Now we need to wrap up our function. First, we will close the %DO loop with an %END. Then, will end the macro with a %MEND.
The finished macro resembles:

%MACRO DATABLOG;
%DO I = 1 %to 5;
%let y = %eval (&I + 1);
If num&I = num&y then VALUE1FLAG = 1;
%END;
%MEND;

So for the sake of examples, I've created the following SAS data set. The name of the set is test1. 




To initialize the macro, simply run the code line:

%<name of the macro>;

However, our macro on its own does very little, as it has no data set to assess. Let's place it in the middle of SAS code block and run the entire program.

Data test2;
Set test1;
%datablog;
Run;


The new data set should resemble the image below:


With this information, you should be armed with the ability to create basic and powerful SAS macros. More SAS macro examples will be displayed in future entries. 


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.