Javatpoint Logo
Javatpoint Logo

What is SAS Macro?

SAS provides a powerful programming feature called Macro. Macro is basically helpful in reusing the code multiple numbers of times at different places so that we do not need to type the entire code repeatedly. It not only reduces time but also generates error-free code.

Macro consists of a set of SAS statements, designated by a particular name so that it can be used anywhere in the program by using that name. Its syntax starts with a %Macro statement and ends with the %MEND statement.

Macro can also be declared in a block of code so that blocks can be reused several times into Macro variables.

SAS Macro

Syntax to create a Macro:

Syntax to Call a Macro:


  • %MACRO: It is a statement used to create Macro.
  • Macro Name: It is a name given to the Macro. We can call Macro anywhere by this name.
  • Param: These are the arguments given to the Macro to perform operations.
  • Macro Statements: These are the SAS in-built Macro Statements that can be used directly.
  • %MEND: It is a statement used to close Macro.
  • Value: These are the values of the variables, given to the Macro to perform operations.

Let's understand Macro through an example:

Here we are creating two data sets. One is a sample, and another is a demo. To run these data sets, we need to use the proc print statement in both data sets. This proc print statement will be written twice in the same program, due to that the length of the code will increase unnecessarily. To reduce this unnecessary length, we use Macro.

In the following code, we are going to create a Macro designated by pri for the proc print statement.


This is a regular code without Macro:

By using Macro, we can reuse this proc print statement. In the below code, we are creating Macro pri for proc print statement so that we can use it where it is needed.

Benefits of SAS Macro

  • It simplifies our work by defining code ones and reusing it multiple times.
  • We can make changes in a variable at a single place, and it will reflect at multiple places.
  • Data of the program is driven, i.e., SAS decides what to do based on actual data values.
  • It reduces the time of the code.
  • It reduces the complexity of the code.

Macro Variables

These variables contain a value that is used repeatedly by a SAS program. We can declare Macro variable at the beginning of a SAS program and called them out later in the body of the program.

The scope of Macro variables can be Global or Local. These are defined below:

Global Macro Variable

Global Macro variables can be accessed by any SAS program available in the SAS environment. Generally, these are system assigned variables that can be accessed by several programs. A typical example of global Macro variable is the system date.


In the following example, we are going to use the global SAS variable SYSDATE, which represents the system date.

Consider a scenario where we need to print system date every day in the title of the SAS report when the report is generated.

The title will show both the current date and day without providing any values for them in the code. Here we are using SAS in-built data set called CARS available in the SASHELP Library.

When we execute the above code in SAS studio, we get the following output.

SAS Macro

Local Macro variable

Local variables can be accessed only within the SAS program in which they are declared. They are generally used to supply different variables in the same SAS statement to process different observations of the data set.



  • % LET: It creates a Macro variable and assigns it a value.
  • Macro Variable Name: It is the name given to the local Macro variable.
  • Value: It is a value field which can take any numeric, text, or date value as per the requirement of the program.


The Macro variables can be declared by using the character & appended at the beginning of the variable name. In the below example, we are going to declare two local variables one is make_name, and another is type_name to fetch the values from CARS data set.

Consider the following code:

When we execute the above code in SAS studio, we get the following output.

SAS Macro

Commonly Used Macros

SAS has many in-built MACRO statements which can be used in any SAS program without declaring them explicitly. Following are the most commonly used Macros:

  • Macro %PUT
  • Macro %RETURN
  • Macro %END

Macro %PUT

This Macro statement is used to write text or Macro variable information into the SAS log. In the below example, we are going to write the value of the variable 'today' into the program log.

Execute the above code in SAS Studio:

SAS Macro


SAS Macro

As per the above code, value "&sysdate"d,worddate22." of variable "today" have been written into the program log.


This Macro statement is used to terminate currently executing Macro when a certain condition is evaluated to be true. In the below example, if the value 1 is assigned to the variable var1, then the Macro will stop the execution, and the DATA step will not execute.

Execute the above code in SAS Studio:

SAS Macro


SAS Macro

As per the above code, the condition has been evaluated as a true, due to which the executing Macro is terminated, and the data step has not been executed.

Macro %END

This Macro statement is used to end a %DO %WHILE loop as per the requirement. We can use it with a %END statement. In the below example, the Macro named demo takes an input 1 and runs the DO loop by using this input value. The DO loop closes with the %End statement while the Macro statement closes with the %mend statement.

Execute the above code in SAS Studio:

SAS Macro


SAS Macro

As per the above code, the value of i has been printed 5 times, and after the completion of 5 times, the %End statement ended the %Do %While loop.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA