DarkousPl
DarkousPl

Reputation: 87

SAS resolve with data step in macro

I have one question and/or prompt. I made and experimenting that code:

/* defined macro */
    %macro makro1(par1);
    %let i = 0;
        %IF &par1. = 1 %then %do; 35 %end;
        %ELSE %IF &par1. = 0 %then %do; 15 %end;
        %ELSE 12;

    %let i = %eval(&i. + 1);
    data _temp_temp&i.;
    _temp = &par1.;
    run;
    %mend makro1;

/* proc sql */
    Proc sql;
    Create table TESCIOR as
    Select
    a.*
    ,resolve('%makro1(' || put(licensed,1.) || ')') as new2
    ,resolve('%makro1(licensed)') as new3
    FROM work._PRODSAVAIL a
    ;quit;

How I can in resolve macro1 (in field new2) run date step "_temp_temp%i." in makro1, and iterate that variable &i. ? I need do calculate in makro1 (which return in field new2) and create table _temp_temp.

Thank for help!

Upvotes: 0

Views: 883

Answers (2)

Richard
Richard

Reputation: 27508

The "Macro Language Reference" documentation explains RESOLVE.

  • Q: What does resolve do ?
  • A: It returns the source code generated by the macro system for the passed expression.

RESOLVE does not execute the generated source code. It will execute as much of the macro instructions as it can. If the passed expression is expected to run a step boundary, resolve is the wrong feature to be using in your code.

The coding of

, resolve('%makro1(' || put(licensed,1.) || ')') as new2
, resolve('%makro1(licensed)') as new3

indicates you may be incorrectly mixing scopes; macro-time versus run-time versus run-time-dynamic.

Suppose your data set has 100 rows. Do you really want to create 200 temp tables ?

Consider looking into DOSUBL if you feel a need to execute generated source code during proc step run-time. Note: Generated source code could be a macro invocation that generates more source code, which will which will also be executed (in the separate synchronous DOSUBL submit stream).

My guess is there is a much simpler solution to what you are really attempting but did not describe in the question.

Upvotes: 1

Tom
Tom

Reputation: 51566

Your macro cannot work as written. Main issue is that it is generating invalid SAS code because it is generating a number before the DATA statement. For example when PAR1=1 it generates this:

35 data _temp....

Secondary issue is that it is always generating dataset named _TEMP_TEMP1 since it always sets I to 0 and then increments it once.

If you want to dynamically call a macro use CALL EXECUTE() function in a data step.

data _null_;
  set work._PRODSAVAIL ;
  call execute(cats('%nrstr(%makro1)(',licensed,')'));
run;

Upvotes: 1

Related Questions