A quick example:
Let’s look at the following SAS program:
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.
In this case, the user does not need to provide value for current date. The code will automatically pick current date from system.
Power of SAS Macros:
SAS macros enable us to:-
- Make our job easier by re-using similar code multiple times after defining it ones.
- Make changes in variable at a single place and reflect them at multiple locations.
- Make your programs data driven, i.e. letting SAS decide what to do based on actual data values
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?
How does Macro Processor Work?
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.
Components of Macro code:
Macro code consists of two basic building blocks: Macros and Macro variables. In a SAS program they are referred differently as:-
- &Name refers to Macro Variable
- %Name refers to Macro
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:-
- It can be of any length between 0 to 65,534 characters
- Numeric values are also stored as character string
- Mathematical expressions are not evaluated
- Quotation mark can also be stored as a part of value
- Leading and trailing blanks are removed before assignment
Macro variables are referenced by using ampersand (&) followed by macro variable name.
&<Macro variable Name>
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.
%Macro <Macro Name>;
Macro Statements can contain combination of the following:-
- SAS statements or steps
- Macro variable references
- Macro statements, expressions or calls
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.
You can see that we have used same set of statements twice using macro.
Adding parameter to SAS Macros:
We can define a Macro with parameters, which can be referenced within the macro. We can pass the parameters in two ways:-
- Positional Parameters
- Keyword Parameters
Positional Parameters: –
In this method, we supply parameter name at time of defining the macro and values are passed at time of Macro call.
At calling stage, values to parameters are passed in similar order as they are defined in the macro definition.
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
- Specified in any order
- Omitted from the call (In this case, the macro will use default value given at definition)
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.
- You want to extract transaction details, Manpower details, Budget details and others for current month only and this is monthly task (Repetitive). You can provide value for current month (between 01-Sep-14 to 30-Sep-14) and with help of Macro Variables, we can create the programme in such a manner that we need to define them only once.
- At initial stage of my career, I used to write SAS codes for data conversion like text to number, number to text, adjusting width of the data type and converting report in specific layout format and others. But with the help of SAS macros, I can write SAS macro for each specific task and call it whenever they are required.
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?”You can also read this article on Analytics Vidhya's Android APP