SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

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


Now that you are familiar with using the CALL SYMPUT routine to assign a value to a macro variable during DATA step execution, there are a few precautions to remember.
  1. First, you cannot reference a macro variable that is created by the CALL SYMPUT routine in the same DATA step in which the macro variable is created.

    Remember that macro variable references are resolved prior to the compilation of a DATA step, and that the CALL SYMPUT routine creates a macro variable during the execution of a DATA step. Because a DATA step compiles and then executes, creating and referencing a macro variable in the same DATA step means that the SAS System searches for the value of the macro variable before it is created.

  2. The second precaution is that 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.


Example

Suppose you had defined the TITLE statement for the PROC PRINT output before the PROC PRINT step, as shown here:

     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;
When submitted, your program creates the following output. Notice the unresolved macro variable reference in the title.


Temporary Employees Worked &total OT Hours

Obs Name Overtime TotalOvertime
1 Cichock, Elizabeth Marie

4

4

2 Bates, Ellen Marie

2

6

3 Abramson, Andrea Carlson

10

16

4 Wallace, Julie Dee

5

21

5 Rusnak, Mona Ann

8

29

6 Bills, Paulette Ann

4

33

7 Wingo, Jennifer Marie

7

40

8 Smith, Jack Kenneth

8

48


You can solve this problem by changing the placement of the TITLE statement in your program. Note that you must place a RUN statement at the end of the DATA step so that the DATA step executes before the TITLE statement is encountered.
 -   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;
 ->  title "Temporary Employees Worked &total OT Hours";
     proc print data=hrd.overtime;
     run;
Temporary Employees Worked 48 OT Hours

Obs Name Overtime TotalOvertime
1 Cichock, Elizabeth Marie

4

4

2 Bates, Ellen Marie

2

6

3 Abramson, Andrea Carlson

10

16

4 Wallace, Julie Dee

5

21

5 Rusnak, Mona Ann

8

29

6 Bills, Paulette Ann

4

33

7 Wingo, Jennifer Marie

7

40

8 Smith, Jack Kenneth

8

48



back||next


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

Terms of Use & Legal Information | Privacy Statement