SAS Macro For Beginners – Macro Variables & Functions
Today we discuss SAS Macros for beginners and how SAS macro makes our life easier by reducing the redundancy or repetitiveness of a code. SAS Macro provides an incredible way to make a process easy and automatic. Here, we will see different SAS Macro Variables, functions of Macro in SAS Programming Language. Moreover, we learn Components of SAS Macro Code and some benefits of SAS Macro
Let’s see how SAS Macro Works.
Keeping you updated with latest technology trends, Join DataFlair on Telegram
1. What is SAS Macro?
Macros basically help reuse code at different places multiple numbers of times without having to type the entire code again and again. This not only saves time but results in error-free code.
Let us understand this with an example:
proc print data=Policysnapshot; where policydate='09Sep14'd; title “Below are the policy detail for 09-Sep-14”; Run;
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.
Now, let us look at the same code by using SAS macros.
Proc Print Data=Sales.Policysnapshot; Where policydate=”&sysdate9”d; Title “Below are the policy detail for %sysdate9”; Run;
In this case, the user does not need to provide value for the current date. The code will automatically pick a current date from a system.
Don’t forget to read – SAS Functions
2. Benefits of Using SAS Macro
SAS macro enable us to:-
- Make our job easier by re-using similar code multiple times after defining it ones.
- Make changes in a variable at a single place and reflect them at multiple locations.
- The data of your program 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.
3. Components of SAS Macro Code
SAS Macro code has two components: Macros and Macro variables. In a SAS program, they have referred differently as:-
- &Name refers to Macro Variable
- %Name refers to Macro
SAS Macro Variables
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 substituting in your program.
The scope of a Macro variable can be local or global, depending on how we have defined it. If it is defined inside a macro program, the 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.
We use %LET statement to create and assign a value to macro variables.
% LET <Macro Variable Name>=Value;
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 a character string
- Mathematical expressions are not evaluated
- Quotation mark can also be stored as a part of a value
- Leading and trailing blanks are removed before assignment
SAS Macro variables are referenced by using an ampersand (&) followed by a macro variable name.
Don’t forget to check – SAS String Functions or Character Functions
&<Macro variable Name>
We can also declare multiple macro variables and use them at different places in SAS Code. The macro variable does not resolve when they access within single quotes. Hence, we should use double quotes to reference them.
is the name of one or more SAS macro variables.
The %GLOBAL statement creates one or more global macro variables and assigns null values to the variables. Global macro variables are variables that are available during the entire execution of the SAS session or job.
A macro variable created with a %GLOBAL statement has a null value until you assign it some other value. If a global macro variable already exists and you specify that variable in a %GLOBAL statement, the existing value remains unchanged.
Must learn – What is SAS Syntax and how we can use them
ii. SAS Local Macro Variables
The syntax of a Local Macro variable in SAS Programming:
|%LOCAL macro-variable-1 <…macro-variable-n>;|
is the name of one or more macro variables
The %LOCAL statement creates one or more local macro variables. A macro variable created with %LOCAL has a null value until you assign it some other value. Local macro variables are variables that are available only during the execution of the macro in which they are defined.
Use the %LOCAL statement to ensure that macro variables created earlier in a program are not inadvertently changed by values assigned to variables with the same name in the current macro. If a local macro variable already exists and you specify that variable in a %LOCAL statement, the existing value remains unchanged.
Understand the concept – SAS Data Set – Special & Built-in Data Set in SAS
iii. Comparison – Global vs Local Variables
- Both the %LOCAL statement and the %GLOBAL statement create macro variables with a specific scope. However, the %LOCAL statement creates local macro variables that exist only during the execution of the macro that contains the variable, and the %GLOBAL statement creates global macro variables that exist for the duration of the session or job.
- If you define a local macro variable and a global macro variable with the same name, the macro facility uses the value of the local variable during the execution of the macro that contains that local variable. When the macro that contains the local variable is not executing, the macro facility uses the value of the global variable.
5. Functions of SAS Macros
These SAS Macro functions have similar syntax, compared to their counterpart functions in data steps and they also return results in a similar manner.
We use this function to convert the case of letters to uppercase:
Syntax:– %UPCASE (Argument)
%Let heading=this is detail of country; Proc Print Data=Orion.Country; Title %UPCASE(&heading); Run;
It will print title as “THIS IS DETAIL OF COUNTRY”.
Explore the top applications of SAS programming
ii. %SUBSTR() Function
This function returns the given number of characters from the specified position.
Syntax:– %SUBSTR (argument, position [, number of characters])
If a number of character is not supplied, a %SUBSTR function will return characters from given position till the end of the string.
%Let abc=this is detail of country; %LET def=%SUBSTR(&abc,19,7); Proc Print Data=Orion.Country; Title %Upcase(&def); Run;
Hence, in this SAS Macros tutorial. I hope you learned some new ways of manipulating data in SAS. You will now be able to save a lot of time by incorporating SAS Macro in your program. Stay tuned to learn more such interesting things in SAS.