SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Improving Program Efficiency with Macro Variables
Creating Your Own Macro Variables


This section teaches you how to create and use your own macro variables. By the end of the section, you will be able to
  • create a macro variable using the %LET statement
  • display log messages stating how macro variable references resolve when using the SYMBOLGEN option
  • combine macro variable references with prefixes and suffixes
  • create a macro variable during DATA step execution by using the CALL SYMPUT routine.


User-Defined Macro Variables

Automatic macro variables enable you to substitute a variety of information in your programs. However, there may be times when you need to include information in programs that automatic macro variables cannot supply, such as a specific text string or the value of a data set variable.

You need to create your own macro variables to supply this type of information. User-defined macro variables enable you to substitute a wider range of text in your programs because you can control the values of the macro variables.


The %LET Statement

The SAS System enables you to create macro variables in several ways, but the simplest way is to use the %LET statement. The %LET statement defines one macro variable at a time and can be used to create a new macro variable or redefine the value of an existing macro variable.


General form, %LET statement:
%LET name=value;

where:

  • name specifies the name of your macro variable
  • value is the value of the macro variable.
NOTE: Do not begin the macro variable name with the letters SYS. The use of this prefix is reserved to SAS software for automatic macro variable names. The name that you define must follow the rules for SAS names. Everything appearing between the equal sign and semicolon is considered part of the macro variable value.


Let's look at an example of a %LET statement. This statement creates a macro variable named region that has the value northwest.
     %let region=northwest;

Notice that the macro variable value is not enclosed in quotes. If quotes are used, they are stored as part of the value. For example, this %LET statement creates the macro variable region with the value 'northwest'.

     %let region='northwest';

Here's another example of a %LET statement. This statement creates a macro variable named level that has the value 768.

     %let level=768;

The value 768 is not treated as a number. All digits, characters, and symbols are processed as text strings.

This %LET statement assigns the value 700+700*.05 to the macro variable rate.

     %let rate=700+700*.05;

The value 700+700*.05 is not evaluated. It is viewed strictly as a text string.



back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement