Let’s look at the following SAS program:
[stextbox id = “grey”]proc print data=Sales.Policysnapshot;
where policydate='09Sep14'd;
title “Below are the policy detail for 09-Sep-14”;
Run;[/stextbox]
Above SAS code is written to extract policy level details for 09-Sep-14 and let us say that the user needs to run this code on a daily basis after changing the date (at both the places) to current date.
Thankfully, SAS – like any other programming language, provides the facility of automating codes with use of SAS macro programming. Now, let us look at the same code once automated by SAS macros.
[stextbox id = “grey”]Proc Print Data=Sales.Policysnapshot;
Where policydate=”&sysdate9”d;
Title “Below are the policy detail for %sysdate9”;
Run;[/stextbox]
In this case, the user does not need to provide value for current date. The code will automatically pick current date from system.
SAS macros enable us to:-
Above all, it helps to reduce the efforts required to read and write SAS Code. Macro programming is typically covered as advanced topic in SAS, but the basic concepts of SAS macros are easy to understand.
In this article, I will introduce the concept of SAS programming and I assume that you are aware about basics of SAS programming. We will look at how macro processor works, how to use SAS macros & macro variables and How to create flexible and reusable code to save time and efforts?
A SAS program is a combination of Data steps, global statements, SAS Component Language (SCL), SQL statements and SAS Macro statements. Whenever we submit a program, it gets copied in memory (called input stack) followed by word scanner and there after it goes to compiler and gets executed. When we use Macros in our SAS program, an additional step is added to resolve macros. This step is called as MACRO Processor.
As we can see above, we have an additional step “Macro Processor” when we work with macro i.e. SAS code with macro does not compile or execute faster. It only helps to reuse the SAS code and variables values.
Macro code consists of two basic building blocks: Macros and Macro variables. In a SAS program they are referred differently as:-
A macro variable is just like a standard variable, except that its value is not a data set and has only a single character value. The value of a macro variable could be a variable name, a numeral, or any text you want substituted in your program.
Scope of Macro variable can be local or global,Add Media depending on how we have defined it. If it is defined inside a macro program, then scope is local (only available for that macro code). However, if we have defined it outside (in the main body), then we can use it anywhere in SAS program.
%LET statement is used to create and assign value to macro variables.
% LET <Macro Variable Name>=Value;
Macro variable name follows the SAS naming convention and if variable already exists then value is overwritten.
Value of macro variable in %Let statement can be any string and it has following characteristics:-
Macro variables are referenced by using ampersand (&) followed by macro variable name.
&<Macro variable Name>
Example:-

We can also declare multiple macro variables and use them at different places in SAS Code. Macro variable are not resolved when they are accessed within single quotes. Hence, we should use double quotes to reference them.
Referencing Macro variable with text
We can reference macro variable with text in multiple ways:-
Let’s look at these with a simple example:-
A period (.) is used as delimiter that defines the end of a macro variable.
Let’s look at an example:-
Here, you can see that the program did not execute because we required (ORION.COUNTRY) and not (ORIONCOUNTRY). This happened because here period (.) is used as separator between two macro variables. Now, to use period as a separator between library name and dataset, we need to provide period (.) twice.
SAS Macros are useful when we want to execute same set of SAS statements again and again. This is an ideal case for use of macro rather than typing / copy pasting same statements.
Macro is a group of SAS statements that is identified by a name and to use it in program anywhere, we can reference it with that name.
Syntax:-
%Macro <Macro Name>;
Macro Statements;
%MEND;
Macro Statements can contain combination of the following:-
After definition of macro, we need to invoke/ call it when want to use it.
Syntax for calling a MACRO: –
%<Macro Name> [;]
Here we do not need to end this statement with a semicolon but it is a good programming practice to have it.
Example:-
You can see that we have used same set of statements twice using macro.
We can define a Macro with parameters, which can be referenced within the macro. We can pass the parameters in two ways:-
In this method, we supply parameter name at time of defining the macro and values are passed at time of Macro call.
Syntax:-
Definition
[stextbox id = “grey”]%MACRO <macro name>(Parameter1, Parameter2,….Parameter-n);
Macro text;
%MEND;[/stextbox]
Calling
[stextbox id = “grey”]%MacroName (Value1, Value2,…..Value-n); [/stextbox]
At calling stage, values to parameters are passed in similar order as they are defined in the macro definition.
Example:-
[stextbox id = “grey”]%MACRO AV(Country_Criteria, City_Criteria);
Proc Print data=Test.sales;
Where Country=”&Country_Criteria” AND CITY=”&City_Criteria”;
Run;
%AV(AU, Sydney);[/stextbox]
In this method, we provide parameter name with equals to sign and also can assign default value to the parameters. While calling the macro, we have to mention the parameter names followed by equals sign and value.
In this method, parameters with equals sign can be
Syntax:-
Definition
[stextbox id = “grey”]%MACRO <macro name> (Parameter1=Value, Parameter2=Value…….Parameter-n=Value);
Macro Text;
%MEND;
[/stextbox]
Calling
[stextbox id = “grey”]%<macro name> (Parameter1=Value, Parameter2=Value …..Parameter-n=Value);
[/stextbox]
Example:-
Let’s discuss some examples of SAS Macro Variable and SAS Macros, when you want to use a particular value multiple times in a program and its value changes over time.
For example:-
SAS Macros are typically considered as part of Advance SAS programming and are used widely in reporting, data manipulation and automation of SAS programs. They do not help to reduce the time of execution, but instead, they reduce repetition of similar steps in your program and enhance the readability of Programs. In next article, we will cover “How to create Conditional and repetitive steps using SAS Macro?”
Great stuff. Tkanks!
Good article. thanks
Although it is usually true that a macro variable created within a macro will exist in the local symbol table, this is NOT always necessarily true, and when it is not true there can be serious consequences (Macro variable collisions). Here is a simple example. %let x = abc; %macro try; %let x=def; %put _user_; %mend try; %try