Basic Concepts

Overview

In the SAS programs that you write, you might find that you need to reference the same variable, data set, or text string multiple times.
title "Total Sales for 2002";
data perm.sales2002;
   set perm.sales;
   if year(enddate)=2002;
run;
proc print data=perm.sales2002;
run;
Then, you might need to change the references in your program in order 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, and it is easy to overlook a reference that needs to be updated.
title "Total Sales for 2001";
data perm.sales2001;
   set perm.sales;
   if year(enddate)=2002;
run;
proc print data=perm.sales2001;
run;
If you use a macro variable in your program, these updates are quick and easy because you need to make the change in only one place.
%let year=2002;
title "Total Sales for &year";
data perm.sales&year;
   set perm.sales;
   if year(enddate)=&year;
run;
proc print data=perm.sales&year;
run;
The value of the macro variable is inserted into your program, so you can make one change and have the change appear throughout the program.

Macro Variables

Macro variables are part of the SAS macro facility, which is a tool for extending and customizing SAS and for reducing the amount of program code that you must enter in order 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 have names. From then on, you can work with the names rather than with the text itself.
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.
The value of a macro variable is stored in a symbol table. The values of automatic macro variables are always stored in the global symbol table, meaning that these values are always available in your SAS session. The values of user-defined macro variables are often stored in the global symbol table as well.
%let city=Dallas;
%let date=05JAN2000;
%let amount=975;
Global Symbol Table
Macro variables can be defined and referenced anywhere in a SAS program except within the data lines of a DATALINES statement. You learn more about how to define and reference macro variables throughout this chapter.

Referencing Macro Variables

In order to substitute the value of a macro variable in your program, you must reference the macro variable. A macro variable reference is created by preceding the macro variable name with an ampersand (&). The reference causes the macro processor to search for the named variable in the symbol table and to return the value of the variable if the variable exists. If you need to reference a macro variable within quotation marks, such as in a title, you must use double quotation marks. The macro processor does not resolve macro variable references that appear within single quotation marks.
Note: You learn more about the macro processor later in this chapter.
Global Symbol Table

Example: Referencing a Macro Variable

To reference the macro variable amount from the global symbol table that is represented above, you place &amount in your program, as follows:
data new;
   set perm.mast;
   where fee>&amount;
run;
proc print;
run;
Code After Substitution
data new;
   set perm.mast;
   where fee>975;
run;
proc print;
run;
Note: You will see representations of code after substitution throughout this chapter. In a SAS session, you do not see this code. These representations are meant to show you what happens to your code behind the scenes, after macro processing.

Example: Referencing a Macro Variable in a Title

To reference the macro variable city in a title, you must use double quotation marks to enclose the title text in the TITLE statement, as follows:
title "Students from &city";
When the macro processor cannot resolve a macro variable reference, a message is printed in the SAS log. For example, referencing a nonexistent macro variable results in a warning message. Referencing an invalid macro variable name results in an error message.
Table 9.1 SAS Log
34 title "Students from &cityst";
WARNING: Apparent symbolic reference CITYST not resolved.
35
36 title "Students from "the_city_in_which_the_student_is_located";
ERROR: Symbolic variable name THE_CITY_IN_WHICH_THE_STUDENT_I must 
       be 32 or fewer characters long.
..................Content has been hidden....................

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