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

Hello,

 

Suppose that my mainfunction call m1, m2 , m3, m4

ex:

 

%macro mainfunc;
%m1;
%m2;
%m3;
%m4;
%m5;

%mend mainfunc;
%mainfunc;

So imagine that in m3, for example, I need to make a libname statement and if the libname does not exist, how can I abort the %mainfunc;

 

1 ACCEPTED SOLUTION

Accepted Solutions
alepage
Barite | Level 11

I have found a good example on the web:

 

 

%macro libexits(libref);
	%if %sysfunc(libref(&libref)) %then
	%do;
		%put "&libref. does not exist";		
		%abort ;
	%end; 
	%else 
	%do;
		
		%put "the path of &libref. is : %sysfunc(pathname(&libref.))";
		
	%end;
	%mend libexits;
	%libexits(test);

View solution in original post

4 REPLIES 4
Reeza
Super User
Is the library a directory or can it be a file?
https://communities.sas.com/t5/SAS-Tips-from-the-Community/SAS-Tip-Check-if-a-directory-exists/td-p/...

You may want to manually add a flag to your macro calls so they only execute if the previous step executed successfully so either create a variable at the end of the process that it completed successfully or rely on SAS automatic macro variables which can vary depending on the process. Usually easier and cleaner to manage manually.
ballardw
Super User

"Does not exist" as in it was supposed to exist before the macro or program runs and doesn't?

Or it was supposed to made in the macro and fails (for some reason)?

Ksharp
Super User

Check function LIBREF() .

 

Examples
This example verifies a libref. If an error or warning occurs, the message is written
to the log. Under some operating environments, the user can assign librefs by using
system commands outside the SAS session.
%if (%sysfunc(libref(sashelp))) %then
%put %sysfunc(sysmsg());
alepage
Barite | Level 11

I have found a good example on the web:

 

 

%macro libexits(libref);
	%if %sysfunc(libref(&libref)) %then
	%do;
		%put "&libref. does not exist";		
		%abort ;
	%end; 
	%else 
	%do;
		
		%put "the path of &libref. is : %sysfunc(pathname(&libref.))";
		
	%end;
	%mend libexits;
	%libexits(test);

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1137 views
  • 3 likes
  • 4 in conversation