Improving Program Efficiency with Macro Variables |
Creating Macro Variables During DATA Step
Execution![]() ![]() |
Another enhancement you can make to your program is to specify when the CALL SYMPUT routine executes. In the program shown, the CALL SYMPUT routine executes during each iteration of the DATA step. |
data hrd.overtime; set hrd.temp(keep=name overtime); if overtime ne .; TotalOvertime+overtime; call symput('total',put(totalovertime,2.)); run; title "Temporary Employees Worked &total OT Hours"; proc print data=hrd.overtime; run; |
With each data step iteration, the CALL SYMPUT routine assigns the current
value of TotalOvertime to the macro variable total .
total is continually updated until the final iteration assigns
a final value. This final value appears in your TITLE statement. |
Obs | Name | Overtime | TotalOvertime |
1 | Cichock, Elizabeth Marie | 4 |
4 |
2 | Bates, Ellen Marie | 2 |
6 |
3 | 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 |
Because the total number of overtime hours for all employees is calculated
during the final iteration of the DATA step, the CALL SYMPUT routine only
needs to execute during this final iteration.
You can control the execution of the CALL SYMPUT routine by
Let's begin with the END= option. You can specify the END= option in the SET statement to create a temporary variable containing an end-of-file indicator. This variable is initialized to 0 before the first iteration of the DATA step and then set to 1 when the SET statement reads the last observation in the data set. Keep in mind that the variable that is created by the END= option is a temporary variable. It is not stored in the new data set.
In this program, the END= option creates the temporary variable
|
data hrd.overtime; set hrd.temp(keep=name overtime) end=last; if overtime ne .; TotalOvertime+overtime; if last=1 then call symput('total',put(totalovertime,2.)); run; title "Temporary Employees Worked &total OT Hours"; proc print data=hrd.overtime; run; |
Note that the IF-THEN statement specifies that, if the value of
last is equal to 1, indicating that the last observation
has been read, then execute the CALL SYMPUT routine. This statement can also
be written as |
if last then call symput('total',put(totalovertime,2.)); |
If the variable last does not have a specified value, the
default is 1. |
![]() ![]() ![]() ![]() ![]() ![]() |
|
![]() |
![]() |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.