Previous Page | Next Page

Interfaces with the Macro Facility

DATA Step Interfaces


Interacting With the Macro Facility During DATA Step Execution

DATA step interfaces consist of eight tools that enable a program to interact with the macro facility during DATA step execution. Because the work of the macro facility takes place before DATA step execution begins, information provided by macro statements has already been processed during DATA step execution. You can use one of the DATA step interfaces to interact with the macro facility during DATA step execution. You can use DATA step interfaces to do the following:

The following table lists the DATA step interfaces by category and their uses.

DATA Step Interfaces to the Macro Facility
Category Tool Description
Execution CALL EXECUTE routine resolves its argument and executes the resolved value at the next step boundary (if the value is a SAS statement) or immediately (if the value is a macro language element).
Resolution RESOLVE function resolves the value of a text expression during DATA step execution.
Deletion CALL SYMDEL routine deletes the indicated macro variable named in the argument.
Information SYMEXIST function returns an indication as to whether the macro variable exists.
Read or Write SYMGET function returns the value of a macro variable during DATA step execution.
Information SYMGLOBL function returns an indication as to whether the macro variable is global in scope.
Information SYMLOCAL function returns an indication as to whether the macro variable is local in scope.
Read or Write CALL SYMPUT routine assigns a value produced in a DATA step to a macro variable.


CALL EXECUTE Routine Timing Details

CALL EXECUTE is useful when you want to execute a macro conditionally. But you must remember that if CALL EXECUTE produces macro language elements, those elements execute immediately. If CALL EXECUTE produces SAS language statements, or if the macro language elements generate SAS language statements, those statements execute after the end of the DATA step's execution.

Note:   Because macro references execute immediately and SAS statements do not execute until after a step boundary, you cannot use CALL EXECUTE to invoke a macro that contains references for macro variables that are created by CALL SYMPUT in that macro.  [cautionend]


Example of Using CALL EXECUTE Incorrectly

In this example, the CALL EXECUTE routine is used incorrectly:

data prices;  /* ID for price category and actual price */
   input code amount;
   datalines;
56 300
99 10000
24 225
;

%macro items;
   %global special;
   %let special=football;
%mend items;

data sales;    /* incorrect usage */
   set prices;
   length saleitem $ 20;
   call execute('%items');
   saleitem="&special";
run;

In the DATA SALES step, the assignment statement for SALEITEM requires the value of the macro variable SPECIAL at DATA step compilation. CALL EXECUTE does not produce the value until DATA step execution. Thus, you receive a message about an unresolved macro variable, and the value assigned to SALEITEM is &special .

In this example, it would be better to eliminate the macro definition (the %LET macro statement is valid in open code) or move the DATA SALES step into the macro ITEMS. In either case, CALL EXECUTE is not necessary or useful. Here is one version of this program that works:

data prices;   /* ID for price category and actual price */
   input code amount;
   datalines;
56 300
99 10000
24 225
;

%let special=football;  /* correct usage */

data sales;
   set prices;
   length saleitem $ 20;
   saleitem="&special";
run;

The %GLOBAL statement is not necessary in this version. Because the %LET statement is executed in open code, it automatically creates a global macro variable. (See Scopes of Macro Variables for more information about macro variable scopes.)


Example of Common Problem with CALL EXECUTE

This example shows a common pattern that causes an error.

/* This version of the example shows the problem. */

data prices;        /* ID for price category and actual price */
   input code amount;
   cards;
56 300
99 10000
24 225
;
data names;     /* name of sales department and item sold */
   input dept $ item $;
   datalines;
BB  Boat
SK  Skates
;

%macro items(codevar=);  /* create macro variable if needed */
   %global special;
   data _null_;
      set names;
      if &codevar=99 and dept='BB' then call symput('special', item);
   run;
%mend items;

data sales;  /* attempt to reference macro variable fails */
   set prices;
   length saleitem $ 20;
   if amount > 500 then
      call execute('%items(codevar=' || code || ')' );
   saleitem="&special";
run;

In this example, the DATA SALES step still requires the value of SPECIAL during compilation. The CALL EXECUTE routine is useful in this example because of the conditional IF statement. But as in the first example, CALL EXECUTE still invokes the macro ITEMS during DATA step execution -- not during compilation. The macro ITEMS generates a DATA _NULL_ step that executes after the DATA SALES step has ceased execution. The DATA _NULL_ step creates SPECIAL, and the value of SPECIAL is available after the _NULL_ step ceases execution, which is much later than when the value was needed.

This version of the example corrects the problem:

/* This version solves the problem. */

data prices;     /* ID for price category and actual price */
   input code amount;
   datalines;
56 300
99 10000
24 225
;

data names;     /* name of sales department and item sold */
   input dept $ item $;
   cards;
BB  Boat
SK  Ski
;
%macro items(codevar=);   /* create macro variable if needed */
   %global special;
   data _null_;
      set names;
      if &codevar=99 and dept='BB' then
         call symput('special', item);
   run;
%mend items;

data _null_;   /* call the macro in this step */
   set prices;
   if amount > 500 then
      call execute('%items(codevar=' || code || ')' );
run;

data sales;    /* use the value created by the macro in this step */
   set prices;
   length saleitem $ 20;
   saleitem="&special";
run;

This version uses one DATA _NULL_ step to call the macro ITEMS. After that step ceases execution, the DATA _NULL_ step generated by ITEMS executes and creates the macro variable SPECIAL. Then the DATA SALES step references the value of SPECIAL as usual.

Previous Page | Next Page | Top of Page