SAS Institute, a leader in data analytics, has produced a software suite called Statistical Analysis System (SAS). SAS can help you with data management activities, business intelligence, advanced analytics, predictive analytics, and multivariate analysis.

Several conglomerates, such as Google, Facebook, Twitter, and Accenture, use SAS to answer the issues that arise at an organizational level, as well as to provide insightful reports. Project management, quality improvement, business forecasting, and operations research are all aided by it. It's especially helpful for graphic design and app development. 

In this article, you will learn all about SAS Macros that help in writing the DRY (Don’t repeat yourself) code.

Data Science Career Boot Camp

The Ultimate Ticket to Top Data Science Job RolesExplore Course
Data Science Career Boot Camp

SAS Macro

SAS includes Macros, a unique programming feature that helps us to avoid writing repetitive code and reuse it when necessary. It also aids in the creation of dynamic variables that can take on changing values in the code depending on how the code is run. In the same way that macro variables can be initialized for blocks of code that will be reused several times, macros can be declared for blocks of code that will be reused multiple times. 

Let us now explore the variables within the SAS Macro.

What Are SAS Macro Variables?

The SAS variables store a value in a SAS program that can be utilized over and over again. These are declared at the beginning of a SAS program and then referenced later in the code. They can have a Local or Global focus.

Before getting into the implementation through examples, we must understand the different types of macro variables. 

So, let us look at the types of macro variables with examples.

What Are Local SAS Macro Variables?

SAS applications can access these variables if they are defined as a part of the program. These are usually used to provide alternative variables in the same SAS statements so that they can process diverse data set observations.


% LET (Macro Variable Name) = Value;

In this, the Value field can include any numeric, text, or date value that the program requires. Any valid SAS variable can be used as the Macro variable name.


The SAS statements utilize the variables by appending the ‘&’ character to the start of the variable name. The program below gathers all of the observations for the brand 'Audi' and the category 'Sports.' If we want a different make result, we must modify the variable make name value without changing anything else in the program. This variable reference can be many times in any SAS statement in the case of bring programs.


%LET make_name = 'Audi';

%LET type_name = 'Sports';

proc print data =;

where make = &make_name and type = &type_name ;

   TITLE "Sales as of &SYSDAY &SYSDATE";




Free Course: Introduction to Data Science

Learn the Fundamentals of Data ScienceEnroll Now
Free Course: Introduction to Data Science

What Are Global SAS Macro Variables?

Because they can be accessible by any SAS program in the SAS environment, these variables are referred to as global macro variables. They are system-assigned variables that are accessed by numerous programs in general. The system time is an example of a global SAS Macro Variable.


Global SAS Macro Variables are system-defined and do not have a specified syntax. 


The SAS variable SYSTIME, which indicates the system time, is illustrated here. Consider the following scenario: every time a millisecond passes by, the SAS report is generated, and the system time is printed in the title. The title will display the current time without any values being coded. We use the SASHELP library's CARS data set, which is an in-built SAS data set.


proc print data =;

where make = 'Audi' and type = 'Sports' ;

   TITLE "Sales as of &SYSTIME";




Let us now learn about Macro Programs in SAS.

What Are SAS Macro Programs?

A macro is a collection of SAS statements that are referred to by a name and can be used in any application that uses that name. The statement begins with a %MACRO and concludes with a %MEND.


In the below syntax, we declare the local variables.

%MACRO <macro name>(Param1, Param2,….Paramn);

Macro Statements;


%MacroName (Value1, Value2,…..Valuen);


The software below displays a set of SAT variables under the show_result macro, which is invoked by other SAS expressions.

%MACRO show_result(make_ , type_);

proc print data =;

where make = "&make_" and type = "&type_" ;

   TITLE "Sales as of &SYSTIME";






Commonly Used SAS Macros

The SAS programming language comes with a number of MACRO statements. 

Some of the common examples are:

  • Terminating a program when a condition is satisfied
  • Recording the runtime value of a variable in the program log

Now, let us look at some of the examples in depth.

SAS Macro %PUT

The SAS log is updated by this macro statement, which writes macro variables or text information. The variable today's value is recorded to the program log in the example below.


data _null_;

CALL SYMPUT ('today',



%put &today;



Data Scientist Master's Program

In Collaboration with IBMExplore Course
Data Scientist Master's Program


When this macro is run, it causes the currently running macro to terminate normally if a specified condition is found to be true. When the variable val’s value reaches 10, the macro quits; otherwise, it continues.


%macro check_condition(val);

   %if &val = 10 %then %return;

   data q;

      Z = 34.2;


%mend check_condition;  




SAS Macro %END

This macro definition includes a %DO %WHILE loop that finishes with a percent END statement, as required. The macro named test in the example below accepts user input and uses it to perform the DO loop. The %end statement marks the end of the DO loop, whereas the %mend statement marks the end of the macro.


%macro test(finish);

   %let i = 1;

   %do %while (&i <&finish);

      %put the value of i is &i;

      %let i=%eval(&i+1);


%mend test;




Become a Better Data Professional With Simplilearn

The SAS Macros allow us to reuse code in various locations without having to repeat it all the time. Not only does this save time but also ensures that the code is error-free.

These are two types of Macro variables, Global, Local, and a Macro program begins with a %MACRO and ends with a %MEND. Some commonly used Macros are - %END, %RETURN, and %PUT.

To learn more about SAS and its many components and use cases, enroll in Simplilearn’s PG in Data Science to master the key data science concepts and tools and become a better data professional.

About the Author


Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors