BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
anmolk171
Calcite | Level 5

I have created a small code but I don't understand why it is not working.

 

%macro sn;
filename x 'C:\SAS\SAS9.1\dquality\sample\*.sas';
CALL EXECUTE (' %INCLUDE "&x"; ');
%mend sn;
%sn;

 

Error:1 CALL Execute('%INCLUDE "C:\SAS\SAS9.1\dquality\sample\dqanalyz.sas";');
----
180
MPRINT(SN): CALL Execute('%INCLUDE "C:\SAS\SAS9.1\dquality\sample\dqanalyz.sas";');

ERROR 180-322: Statement is not valid or it is used out of proper order.

MLOGIC(SN): Ending execution.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You defined a fileref named X and then later tried to use a macro variable named X that you never defined. 

 

You cannot use CALL EXECUTE() outside of a DATA step.  Your program does appear to need a data step.  It also doesn't need to be wrapped in a macro definition either.

 

Instead just use the fileref in the %INCLUDE statement.

filename x 'C:\SAS\SAS9.1\dquality\sample\*.sas';
%INCLUDE X ;

Or skip the FILENAME and use the path in the %INCLUDE statement.

%INCLUDE 'C:\SAS\SAS9.1\dquality\sample\*.sas';

You could also point the fileref at the folder.

filename x 'C:\SAS\SAS9.1\dquality\sample\';
%INCLUDE X('*.sas');

But watch out if you are using Windows because of how it handles extensions longer than three characters.  The pattern *.sas will also match *.sas7bdat and *.sas7bcat.

View solution in original post

11 REPLIES 11
jimbarbour
Meteorite | Level 14

 

%let X = %STR('C:\SAS\SAS9.1\dquality\sample\*.sas');

%macro sn; CALL EXECUTE (' %INCLUDE "&x"; '); %mend sn; %sn;

Try taking the filename out of the macro and making it a %LET as shown.

 

And %sn needs to be inside a Data step, like so:

DATA	_NULL_;
    %sn;
RUN;

Jim

 

anmolk171
Calcite | Level 5
Thanks Jim for your response but I am getting the same error
jimbarbour
Meteorite | Level 14

First make sure you moved %sn inside a DATA step.

 

Then, please post your code and your log using the buttons in the reply window.

jimbarbour_0-1602617538766.png

 

Jim

jimbarbour
Meteorite | Level 14

Oh, wait.  I see the problem.  You have an asterisk in your "X".  You can't do that.

 

Let me code something up.

 

Jim

jimbarbour
Meteorite | Level 14

OK, try this code (below).  CAUTION:  This code will execute every program in the folder you point to.  You are going to run every program in that directory if you use this code.

 

You need to double check that I've set the Path and the Drive correctly.

 

Jim

 

%LET	Path	=	C:\SAS\SAS9.1\dquality\sample;
%LET	Drive	=	C;

FILENAME	Pgm_List	PIPE	"cd ""&Path"" && &Drive: && dir *.sas /a /b";

DATA	_NULL_;
	INFILE	Pgm_List;
	INPUT	Pgm_Name	:	$256.;
	Pgm_Name	=	CATS("&Path", "\", Pgm_Name);
	PUTLOG	"NOTE:  "  Pgm_Name=;
	CALL EXECUTE (CATX(' ', '%INCLUDE"', Pgm_Name, '";'));
RUN;
Reeza
Super User
What are you trying to do here?
Wouldn't the following do what you want? Why the need for the CALL EXECUTE?

%include 'C:\SAS\SAS9.1\dquality\sample.*.sas';
jimbarbour
Meteorite | Level 14

@Reeza wrote:
What are you trying to do here?
Wouldn't the following do what you want? Why the need for the CALL EXECUTE?

%include 'C:\SAS\SAS9.1\dquality\sample.*.sas';

@Reeza,

 

Would that work with a wildcard like that?  

 

I would agree that a CALL EXECUTE isn't strictly necessary here.  I have used CALL EXECUTE to format the commands for a SYSTASK to launch a bunch of SAS jobs that each write their own log and essentially function as independent jobs once launched.  I've found that to be useful.

 

Jim

Reeza
Super User
Yes, %INCLUDE and FILENAME both allow the wildcard in my experience.
jimbarbour
Meteorite | Level 14

Oh, fantastic.  I've used a wildcard on a Filename before but never an Include.  Thank you for that.  Perhaps it will come in handy some day.

 

Jim

anmolk171
Calcite | Level 5
I have found another workaround to do this but really appreciate it if you guys can tell what's wrong with the code.

Thanks!
Anmol
Tom
Super User Tom
Super User

You defined a fileref named X and then later tried to use a macro variable named X that you never defined. 

 

You cannot use CALL EXECUTE() outside of a DATA step.  Your program does appear to need a data step.  It also doesn't need to be wrapped in a macro definition either.

 

Instead just use the fileref in the %INCLUDE statement.

filename x 'C:\SAS\SAS9.1\dquality\sample\*.sas';
%INCLUDE X ;

Or skip the FILENAME and use the path in the %INCLUDE statement.

%INCLUDE 'C:\SAS\SAS9.1\dquality\sample\*.sas';

You could also point the fileref at the folder.

filename x 'C:\SAS\SAS9.1\dquality\sample\';
%INCLUDE X('*.sas');

But watch out if you are using Windows because of how it handles extensions longer than three characters.  The pattern *.sas will also match *.sas7bdat and *.sas7bcat.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 1157 views
  • 1 like
  • 4 in conversation