| 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.
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;
|