Introducing Macro Variables

A Brief Overview

The SAS macro facility is a tool for extending and customizing SAS. It also reduces the amount of program code that you enter to perform common tasks. The macro facility has its own language, which enables you to package small or large amounts of text into units that are assigned names called macro variables. After a macro variable is created, you can insert the text into your code simply by referencing the macro variable by its name.
SAS macro variables enable you to substitute text in your SAS programs. Macro variables can also provide the following information:
  • operating system
  • SAS session
  • text strings
When you reference a macro variable in a SAS program, SAS replaces the reference with the text value that has been assigned to that macro variable. By substituting text into the program, SAS macro variables make your programs more reusable and dynamic.
There are two types of macro variables:
  • automatic macro variables, which are provided by SAS
  • user-defined macro variables, whose values you create and define
Whether automatic or user-defined, a macro variable is independent of a SAS data set and contains one text string value that remains constant until you change it. The value of a macro variable is substituted into your program wherever the macro variable is referenced.
Macro variables are stored in the global symbol table that makes the values available in your SAS session.
Macro variables can be defined and referenced anywhere in a SAS program except within the data lines of a DATALINES statement.

Example: Using Macro Variables

In a SAS program, you might need to reference the same variable, data set, or text string multiple times.
title "Employees Hired in 2012";
data work.emp2012;
   set certadv.empdata;
   if year(HireDate)=2012;
run;
proc print data=work.emp2012;
run;
Later, you might need to reference a different variable, data set, or text string. Especially if your programs are lengthy, scanning for specific references and updating them manually can take a lot of time. It is also easy to overlook a reference that needs to be updated.
title "Employees Hired in 2011";
data work.emp2011;
   set certadv.empdata;
   if year(HireDate)=2011;
run;
proc print data=work.emp2011;
run;
If you need to change your program, macro variables make these updates quick and easy because you make the change in only one place. To change the value of the macro variable in the code below, change the value of the %LET statement from 2012 to 2011.
%let year=2012;
title "Employees Hired in &year";
data work.emp&year;
   set certadv.empdata;
   if year(HireDate)=&year;
run;
proc print data=work.emp&year;
run;
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.28.93