简体   繁体   中英

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;

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM