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: |
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: |
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:
If the suffix begins with a period, use
two periods between the two parts. Example:
|
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;
|
|
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
|
![]() ![]() ![]() ![]() ![]() |
|
![]() |
![]() |
Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.