DATA Step, Macro, Functions and more

%include - conditional

Reply
Super Contributor
Posts: 291

%include - conditional

I have a program that runs at various times that contains an %include statement to call in another program. I'd like the %include statement to execute conditionally. I don't have correct syntax and I get the error indicated. What is the correct syntax for this? Thanks. (SAS9.1 on Windows)

815 data _null_;
816 if day(today()) in (1,5) then
817 %include "\\technology\AMLogo.sas";
818 +**********
--
180

ERROR 180-322: Statement is not valid or it is used out of proper order.
Super Contributor
Super Contributor
Posts: 3,174

Re: %include - conditional

The approach taken will depend - if you have conditional code to execute within the current DATA step, wrap it in an IF THEN DO; ; END;

Also, review the CALL EXECUTE command in DATA step programming, however that code will not execute until the current DATA step completes. Suggest coding a RUN; to terminate DATA steps, especially with this coding situation.

Scott Barry
SBBWorks, Inc.
Super Contributor
Posts: 291

Re: %include - conditional

Thanks Scott, for the call execute idea. I didn't know about that one. Here's what I've come up with:

%macro logo;
%include "\\technology\AMLogo.sas";
%mend logo;


data _null_;
if day(today()) in (1,7) then call execute('%logo');
run;
Frequent Contributor
Posts: 102

Re: %include - conditional

It is my understanding that the %INCLUDE statement is a compile time statement and cannot be conditionaly executed by a data step. It will need to be handled in the MACRO language. Something like this:
-----------------------------------------
%macro AMLogoTest;
%let today = %sysfunc(day(%sysfunc(today())));
%put Today = &Today;
%if &today = 1 or &today = 5 %then %do;
%put Run the AMLogo code;
%include "\\technology\AMLogo.sas";
%end;
%mend;
%AMLogoTest;
SAS Super FREQ
Posts: 8,864

Re: %include - conditional

Posted in reply to CurtisMack
And, to add to what Curtis suggests, this user group paper has a very good intro to Macro concepts and Macro conditional logic:
http://www2.sas.com/proceedings/sugi28/056-28.pdf
cynthia
Frequent Contributor
Posts: 102

Re: %include - conditional

Posted in reply to Cynthia_sas
Thanks Cynthia,
My solution can of course be shorted to:
-----------------------------
%macro AMLogoTest;
%if %sysfunc(day(%sysfunc(today()))) = 1 or %sysfunc(day(%sysfunc(today()))) = 5 %then %include "\\technology\AMLogo.sas";
%mend;
%AMLogoTest; Message was edited by: CurtisMack
Super Contributor
Super Contributor
Posts: 3,174

Re: %include - conditional

Posted in reply to CurtisMack
Why do you need to complicate matters by introducing a macro invocation, presuming that you have SAS DATA step code in your program file \\technology\AMLogo.sas -- especially the lack of SAS statement numbers when using macros.

A simple DO; / END; construct surrounding your %INCLUDE statement makes it oh so very simple and easier to maintain, debug, and understand.

What's unclear - at least to me - is what type of SAS code is in your %INCLUDE file?

Also, the very powerful RESOLVE DATA step function may be the ticket if you in fact have code that must be compiled on the fly as part of the DATA step processing. Here is an example:

1 %macro echoinfo;
2 %global mystatus;
3 %put right now, macro var mystatus is: &mystatus;
4 %mend echoinfo;
5 data _null_;
6 call symput('mystatus','I am executing inside this DATA step!');
7 x = resolve('%echoinfo');
8 run;

right now, macro var mystatus is: I am executing inside this DATA step!
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.01 second


Scott Barry
SBBWorks, Inc.
Frequent Contributor
Posts: 102

Re: %include - conditional

This is basicaly a style issue, and I posted my reply before seeing yours.

That said, I did not want to assume that the code in his included file could execute within the data step. I personaly see no reason to call the data step just to call some simple functions. In my opinion if all that is needed is some conditional execution of code, the call execute method is more confusing than the equivalent macro solution. Mostly because it is too easy to assume that the code is executing durring the data step instead of after it.

The resolve function is a good solution in many places, and can execute within the datastep. It does make my comment about executing a %include inside a data step partially incorrent. But as I said I did not want to assume what kind of code was inside that file and resolve assumes the contents would return a string that can be store inside a variable. The logic might have been much more complex. I also think that in this example it is harder to follow the logic.

IMHO
Curtis
Super Contributor
Super Contributor
Posts: 3,174

Re: %include - conditional

Posted in reply to CurtisMack
To CurtisMack - I am sorry about not being specific with my reply - please understand it was not to criticize your post. I apologize since it may have been misconstrued.

Scott Barry
SBBWorks, Inc.
Frequent Contributor
Posts: 102

Re: %include - conditional

Scott,
I just saw it as a difference of opinion that we were expressing. You made no disparaging remarks, but thanks for clarifying things.
Curtis
Ask a Question
Discussion stats
  • 9 replies
  • 246 views
  • 0 likes
  • 4 in conversation