Victor
Victor

Reputation: 17077

SAS define a macro inside a data step

I have some legacy SAS code that defines a macro inside a data step. How does this work? Does the macro get compiled for every iteration of DATA step?

    data test;
    set temp;
    %macro cal_sum(iput);
    %let a=&input.;
    %mend;
    REPORTING_DATE = &PROCDATE.;
    call execute('%cal_sum(testval)');
    run;

Upvotes: 0

Views: 973

Answers (2)

Richard
Richard

Reputation: 27498

The macro code, in this case a macro definition, gets fully resolved before the data step is compiled and run by the SAS executor.

So no, the macro is not compiled for every iteration of the DATA step.

Also, as you my be aware, a macro call coded inside a data step is not invoked for every iteration, however, whatever source code the macro call emitted will be.

Upvotes: 1

Allan Bowe
Allan Bowe

Reputation: 12691

No. In this case, the macro is compiled before the data step executes. There are only three ways you can run / compile a macro for every iteration of a data step:

  1. dosubl()
  2. run_macro within an fcmp call
  3. call execute - but note that any generated SAS code is executed AFTER the data step.

The way it works is:

data test; set temp; is sent to the stack, ready to be executed on the next step boundary

%macro cal_sum(iput);%mend; macro is compiled (not executed)

REPORTING_DATE = &PROCDATE.;run; is sent to the stack, and executed (as the run; statement is a step boundary)

Upvotes: 1

Related Questions