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.

Become a Data Scientist with Hands-on Training!

Data Scientist Master’s ProgramExplore Program
Become a Data Scientist with Hands-on Training!

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.

Syntax

% 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.

Example

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.

Code

%LET make_name = 'Audi';

%LET type_name = 'Sports';

proc print data = sashelp.cars;

where make = &make_name and type = &type_name ;

   TITLE "Sales as of &SYSDAY &SYSDATE";

run;

Output

SAS_Macro_Guide_1

Become a Data Scientist with Hands-on Training!

Data Scientist Master’s ProgramExplore Program
Become a Data Scientist with Hands-on Training!

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.

Syntax

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

Example

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.

Code

proc print data = sashelp.cars;

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

   TITLE "Sales as of &SYSTIME";

run;

Output

SAS_Macro_Guide_2

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.

Syntax

In the below syntax, we declare the local variables.

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

Macro Statements;

%MEND;

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

Example

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 = sashelp.cars;

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

   TITLE "Sales as of &SYSTIME";

run;

%MEND;

%show_result(BMW,SUV);

Output

SAS_Macro_Guide_3

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.

Code

data _null_;

CALL SYMPUT ('today',

TRIM(PUT("&sysdate"d,worddate22.)));

run;

%put &today;

Output 

SAS_Macro_Guide_4

Become a Data Scientist with Hands-on Training!

Data Scientist Master’s ProgramExplore Program
Become a Data Scientist with Hands-on Training!

SAS Macro %RETURN

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.

Code

%macro check_condition(val);

   %if &val = 10 %then %return;

   data q;

      Z = 34.2;

   run;  

%mend check_condition;  

%check_condition(11);

Output 

SAS_Macro_Guide_5.

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.

Code

%macro test(finish);

   %let i = 1;

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

      %put the value of i is &i;

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

   %end;

%mend test;

%test(5)

Output 

SAS_Macro_Guide_6

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.

Data Science & Business Analytics Courses Duration and Fees

Data Science & Business Analytics programs typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Professional Certificate in Data Analytics and Generative AI

Cohort Starts: 26 Nov, 2024

22 weeks$ 4,000
Professional Certificate Program in Data Engineering

Cohort Starts: 2 Dec, 2024

7 months$ 3,850
Post Graduate Program in Data Analytics

Cohort Starts: 6 Dec, 2024

8 months$ 3,500
Post Graduate Program in Data Science

Cohort Starts: 9 Dec, 2024

11 months$ 3,800
Caltech Post Graduate Program in Data Science

Cohort Starts: 24 Feb, 2025

11 months$ 4,000
Data Scientist11 months$ 1,449
Data Analyst11 months$ 1,449