SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Improving Program Efficiency with Macro Variables
Lesson Summary

This page contains

I. Text Summary

To go to the page where a task, programming feature, or concept was presented, select a link.


Purpose of Macro Variables
A macro variable simplifies the process of editing multiple references in a program. By using a macro variable to substitute text, you can make a single change appear throughout a program.

Types of Macro Variables
There are two types of macro variables: automatic and user-defined. Automatic macro variables are provided by SAS software; user-defined macro variables can be created as needed.

Automatic Macro Variables
Whenever you run SAS software, automatic macro variables are created that provide information such as the date a SAS job began executing or the name of the most recently created data set. To reference any macro variable, specify the macro variable name preceded by an ampersand (&).

Commonly Used Automatic Macro Variables
      SYSDATE9 - date SAS job or session began executing
      SYSDATE - date SAS job or session began executing
      SYSDAY  - weekday SAS job or session began executing
      SYSTIME - time SAS job or session began executing
      SYSSCP  - operating system abbreviation
      SYSVER  - SAS software version and/or release number
      SYSLAST - name of most recently created data set

User-Defined Macro Variables
To supply a text string or the value of a data set variable, write a user-defined macro variable. Then each time you run the program, you can redefine the value of the macro variable as it appears throughout the code.

The %LET Statement
The simplest way to create a macro variable is by using the %LET statement. It begins with the percent sign (%), followed by the keyword LET, followed by the name of the macro variable, an equal sign (=), and the value of the variable. Example: %let region=northwest;

SYMBOLGEN System Option
To include messages in the SAS log about the resolution of macro variable references, add the SYMBOLGEN option to your program. This can help you verify the values of macro variables.

Combining with Prefixes
Macro variables can be added to other text strings to form composite text, such as data set names. Just place the prefix immediately before the macro variable reference. Example: data hrd.temp&yr

Combining with Suffixes
Macro variables can also be combined with suffixes, but be sure to place a period between the macro variable name and the text string that follows. Example: &period.date

If the suffix begins with a period, use two periods between the two parts. Example: &libref..temp&yr

CALL SYMPUT Routine
To assign a value to a macro variable during DATA step execution, use the CALL SYMPUT routine. The general form of the routine is CALL SYMPUT(name,value), where name is a character variable or a string enclosed in quotes, and value is a text string, data set variable, or DATA step expression.

Controlling Execution of CALL SYMPUT
Often, the CALL SYMPUT routine is only needed at the last iteration of a DATA step. To control its execution, use the END=option in the SET statement and place the CALL SYMPUT routine in an IF-THEN statement.


II. Syntax

To go to the page where a statement or an option was presented, select a link.

%LET name=value;

OPTIONS NOSYMBOLGEN | SYMBOLGEN;

CALL SYMPUT( name,value);

END=variable-name


III. Sample Programs
     title 'Temporary Employees Hired in November';
     footnote "Report Run on &sysday, &sysdate";
     data hrd.tempnov;
        set hrd.temp;
        if month(begindate)=11;
     run;
     proc print data=hrd.tempnov;
     run;

     title "Temporary Employees for &year";
     footnote;
     options symbolgen;
     %let year=1999;
        data hrd.newtemp;
        set hrd.temp;
        if year(enddate)=&year;
     run;
     proc print data=hrd.newtemp;
     run;

     title "Temporary Employees for &yr";
     %let yr=1999;
     data hrd.temp&yr;
        set hrd.temp;
        if year(enddate)=&yr;
     run;
     proc print data=hrd.temp&yr;
     run;

     title "Temporary Employees for &yr";
     %let yr=1999;
     %let period=end;
     %let libref=hrd;
     data &libref..temp&yr;
        set &libref..temp;
        if year(&period.date)=&yr;
     run;
     proc print data=&libref..temp&yr;
     run;

     title "Temporary Employees Worked &total OT Hours";
     data hrd.overtime;
        set hrd.temp(keep=name overtime) end=last;
        if overtime ne .;
        TotalOvertime+overtime;
        if last then call
           symput('total',put(totalovertime,2.));
     run;
     proc print data=hrd.overtime;
     run;


IV. Points to Remember
  • Macro variables can be defined and referenced anywhere in a SAS program, except within data lines.

  • Macro variable references that appear in quoted strings must be enclosed in double quotes. If single quotes are used, the macro variable reference will not be resolved.

  • Because SAS software uses the SYS prefix for automatic macro variables, you should avoid beginning the name of any user-defined macro variable with the letters SYS.

  • When creating a user-defined macro variable, all digits, characters, and symbols are processed as text strings.

  • If the SAS log complains about unresolved macro variable references, check that the macro variable references are spelled correctly and that you haven't referenced a macro variable before defining it.

  • Assigning values to macro variables by using the CALL SYMPUT routine can sometimes result in leading blanks. This is due to automatic numeric-to-character conversion. Remove the blanks by explicitly converting numeric values using a PUT function in the CALL SYMPUT routine.

  • You cannot reference a macro variable that is created by the CALL SYMPUT routine in the same DATA step that creates the macro variable.

  • You cannot reference a macro variable that is created by the CALL SYMPUT routine in a global statement (such as a TITLE statement) that precedes the DATA step in which the macro variable is created.


back||next

 

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

Terms of Use & Legal Information | Privacy Statement