DATA Step, Macro, Functions and more

stored macro execution inside a macro called with CALL EXECUTE routine

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 127
Accepted Solution

stored macro execution inside a macro called with CALL EXECUTE routine

Hi,

Somebody knows why %missvars works fine in a normal program

     %missvars(ds); /* http://www.datasavantconsulting.com/roland/missvars.sas */

and doesn't in a macro called with call execute routine

     %macro test;

          %missvars(ds

     %mend test;

     data _null_;

          set NRC_germ;

          call execute('%test('||trim(Germ)||')');

          run;

?


Accepted Solutions
Solution
‎06-24-2013 09:19 AM
Respected Advisor
Posts: 3,799

Re: stored macro execution inside a macro called with CALL EXECUTE routine

You probably need to delay execution of the CALL EXECUTEd macro until after the data step that is doing the CALL EXECUTEing finishes.

call execute('%nrstr(%germvars('....

Message was edited by: data _null_

View solution in original post


All Replies
Super User
Posts: 10,035

Re: stored macro execution inside a macro called with CALL EXECUTE routine

You don't have argument in your macro.

%macro test(ds);

          %missvars(&ds)

     %mend test;

Frequent Contributor
Posts: 127

Re: stored macro execution inside a macro called with CALL EXECUTE routine

Right,

but this is just a simplified code I wrote quickly. No repercussions here.

This is the actual code if you need it (but it'll confuse the question)

(I'am trying to change %missvars with %dropmiss right now but it's the same issue : it produces weird logs with no errors (cf. attached log) ) :

data NRC_GERMVARIABLE(where=(Germ ne ""));

  length Source Germ Variable $30;

  run;

%macro germvars(subject);

  %put 0--- subject=&subject;

  %put 1--- select germ ;

  data WORK.NRC_VARIABLES1;

  set ID_DATA.NRC_CLEAN;

  where Subject="&subject";

  run;

  %put 2--- remove empty variables ;

/* %missvars(NRC_VARIABLES1);

  %put &_miss_;

  data NRC_VARIABLES2;

  set NRC_VARIABLES1(drop=&_miss_);

  run;*/

  %dropmiss(NRC_VARIABLES1,NRC_VARIABLES2);

  %put 3--- list variables vertically ;

  proc transpose

  data= WORK.NRC_VARIABLES2 (obs=1)

  out= WORK.NRC_VARIABLES3(rename=(_NAME_=Variable) drop=_LABEL_);

  var _all_;

  run;

  %put 4--- append them in NRC_GERMVARIABLE ;

  data  WORK.NRC_VARIABLES4 (drop=col1);

  length col1 $20;

  set  WORK.NRC_VARIABLES3;

  col1=col1;

  Source="NRC";

  Germ="&subject";

  run;

  PROC APPEND BASE=NRC_GERMVARIABLE DATA= WORK.NRC_VARIABLES4 FORCE NOWARN;

/* */

%mend germvars;

data _null_;

  set NRC_germ;

  call execute('%germvars('||trim(Germ)||')');

  run;

(this forum still needs sas syntaxing)

from the log it looks like de the order of execution is not right.

Attachment
Solution
‎06-24-2013 09:19 AM
Respected Advisor
Posts: 3,799

Re: stored macro execution inside a macro called with CALL EXECUTE routine

You probably need to delay execution of the CALL EXECUTEd macro until after the data step that is doing the CALL EXECUTEing finishes.

call execute('%nrstr(%germvars('....

Message was edited by: data _null_

Super User
Posts: 5,511

Re: stored macro execution inside a macro called with CALL EXECUTE routine

CALL EXECUTE is tricky.  The standard actions it takes are to perform any macro language statements immediately.  However, any generated DATA and PROC statements have to wait until the currently executing DATA step completes.  So all your %PUT statements execute right away, before the generated DATA and PROC steps have run.

Your code is probably breaking down at the point this statement appears:  %put &_miss_;

We don't see the definition of the interior macros, but it is fairly certain that this %PUT statement executes before &_miss_ has been created.  Without seeing the macros nor the error messages, it's a little difficult to say.  But it seems likely that this is the issue.

The usual remedy to delay macro execution when using CALL EXECUTE is to apply the %NRSTR function within the CALL EXECUTE statement.

Good luck.

Frequent Contributor
Posts: 127

Re: stored macro execution inside a macro called with CALL EXECUTE routine

Posted in reply to Astounding

That's it, %NRSTR


Thanks !

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 242 views
  • 3 likes
  • 4 in conversation