DATA Step, Macro, Functions and more

how to call execute multiple macros in if/else statement

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

how to call execute multiple macros in if/else statement

data _null_;

if 1=1 then

call execute('%extractTable();');

call execute('%scoreUpdate()');

call execute('%writeTable();');

call execute(

       'proc sql noprint;

           insert into tableA(date,tag)

            set date=today(),

             tag=1;');

else call execute('%writeIndex();');

run;

 

return Error 160-185:no match if-then statement

 

the problem is caused  calling execute multiple macros..please fix the sas codes..thanks!


Accepted Solutions
Solution
‎03-23-2018 05:36 AM
Super User
Posts: 6,542

Re: how to call execute multiple macros in if/else statement

One version:

 

data _null_;

if 1=1 then

call execute

        ('  %nrstr( %extractTable() %scoreUpdate() %writeTable() )

            proc sql noprint;

            insert into tableA(date,tag)

            set date=today(),

             tag=1;

       ');

else call execute('%nrstr(%writeIndex())');

run;

 

It's possible that adding %NRSTR is overkill and unnecessary.  But there's no way to know without seeing the contents of the four macros.

 

Consider whether a QUIT statement should be added at the end of PROC SQL.

View solution in original post


All Replies
Super Contributor
Posts: 320

Re: how to call execute multiple macros in if/else statement

Hello,

Since you are not reading values from an input dataset, there is no need here for a data step with call execute instructions. You can directly call your macros :

%extractTable();
%scoreUpdate()
...
Super Contributor
Posts: 320

Re: how to call execute multiple macros in if/else statement

As for the error you mentionned, if several instructions have to be executed when a given condition is met, they have to be enclosed by do;...end;

 

if (<condition>)  then do;

  <instruction1>

  <instruction2>

  ...

end;

Solution
‎03-23-2018 05:36 AM
Super User
Posts: 6,542

Re: how to call execute multiple macros in if/else statement

One version:

 

data _null_;

if 1=1 then

call execute

        ('  %nrstr( %extractTable() %scoreUpdate() %writeTable() )

            proc sql noprint;

            insert into tableA(date,tag)

            set date=today(),

             tag=1;

       ');

else call execute('%nrstr(%writeIndex())');

run;

 

It's possible that adding %NRSTR is overkill and unnecessary.  But there's no way to know without seeing the contents of the four macros.

 

Consider whether a QUIT statement should be added at the end of PROC SQL.

Super User
Posts: 9,604

Re: how to call execute multiple macros in if/else statement

Posted in reply to Astounding

The use of %nrstr depends on the contents of the macros. If they only have macro variables/parameters used, %nrstr is not needed. But if there's logic in there (%if %then %else), or steps that create macro variables for later use, you can get into timing problems, as macro statements will be executed as soon as they are pushed onto the execution stack, while data or proc steps cannot run before the current data step finishes.

%nrstr prevents such premature resolution of macros.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 107 views
  • 1 like
  • 4 in conversation