SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Improving Program Efficiency with Macro Variables
Creating Macro Variables During DATA Step Execution


In this lesson so far, you have used the %LET statement and macro variable references to substitute a variety of information in your programs. You created macro variables to contain a year value, a libref, and a portion of a variable name.

The %LET statement can define most of the macro variables needed for your programs. However, if you need a macro variable to contain a value that is created during the execution of the DATA step, the %LET statement cannot define this macro variable.

For example, suppose you need to create a data set that contains the total number of overtime hours worked by temporary employees. The DATA step you write calculates total overtime hours, but how do you create a macro variable to hold this value? You cannot use a %LET statement for this purpose because you do not know the value to specify in the statement.


The CALL SYMPUT Routine

You can use the CALL SYMPUT routine to create a macro variable whose value is assigned during the execution of the DATA step. To learn how to use this routine, let's look more closely at your DATA step.

The DATA step below creates the new data set Hrd.Overtime and uses the subsetting IF statement to include only the names of employees who worked overtime hours. It also calculates the total number of overtime hours.

     data hrd.overtime;
        set hrd.temp(keep=name overtime);
        if overtime ne .;
        TotalOvertime+overtime;
     run;
     proc print data=hrd.overtime;
     run;

Before submitting the program, you decide to add a title displaying the total number of overtime hours worked by temporary employees. So, you add a TITLE statement containing a macro variable reference to substitute the total number of hours into the title.

     title "Temporary Employees Worked &total OT Hours";

To create the macro variable total, you must use the CALL SYMPUT routine. The CALL SYMPUT routine enables you to assign a value that is produced in the DATA step to a macro variable. The CALL SYMPUT routine is a CALL routine, which is a program you can run from the DATA step. Like all CALL routines, the CALL SYMPUT routine must be invoked by means of a CALL statement.


General form, CALL statement for CALL SYMPUT routine:
CALL SYMPUT( name,value);

where

  • CALL is the keyword.

  • SYMPUT invokes the SYMPUT routine.

  • name is the name of the macro variable to be defined. The variable can be a character string enclosed in quotes, a character variable, or a character expression.

  • value is the value to be assigned to the macro variable. The value can be a text string enclosed in quotes, a data set variable, or a DATA step expression.



  back||next


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

Terms of Use & Legal Information | Privacy Statement