BookmarkSubscribeRSS Feed
_Dan_
Quartz | Level 8

Here's my code:

%sysfunc(ifc(%sysfunc(libref(LIBNAME))
		,%nrstr(%put ERROR: LIBNAME Libref does not exist.; %goto exit;)
		,%nrstr(%put NOTE: LIBNAME Libref exists.;)	));

My issue is of course that the %NRSTR is causing an issue with the %goto statement:

ERROR: The %GOTO statement is not valid in open code.

%NRSTR must exist to allow the 'ifc' to properly run the true/false statements. Without %NRSTR, ifc executes all of the statements regardless of the outcome.

 

%unquote, %bquote %nrbquote %str etc etc have all been used to varying degrees of failure. Any idea how I break that portion of the code out of the %NRSTR to allow the %GOTO to work?

9 REPLIES 9
RW9
Diamond | Level 26 RW9
Diamond | Level 26

No, I think your problem is in understanding the difference between Base SAS code and Macro code.   The log is telling you that you can't do macro code in datastep.  What you likely want is:

%if %sysfunc(libref(LIBRARY)) %then %do;
  %put NOTE: LIBNAME Libref exists;
%end;
%else %do;
  %put ERROR: LIBNAME does not exist;
%end;
_Dan_
Quartz | Level 8
This entire process runs within a %macro, my understanding is fine. %NRSTR is affecting the compilation of the %goto.
Astounding
PROC Star

I can't promise how this will work in the context of your macro, but the usual way to mask the effect of % within %NRSTR is to refer to the % as %%:

 

,%nrstr(%%put ERROR: LIBNAME Libref does not exist.; %%goto exit; )

 

There is the possibility of needing to %unquote, but first try it without %unquote to see if that will work.

_Dan_
Quartz | Level 8

Hi there

 

So basically everything works apart from the %goto. I've had various methods of running this little checker, usually the end result would be a %let statement instead of this %goto. That works fine.

 

Essentially, the only problem is the %goto. There's no need for double %% because I'm not trying to escape anything (that's not what NRSTR is doing here). In fact, I want the %GOTO to work, and in theory it does, but SAS is seeing it outside of the context of it being inside of a %macro. I could literally write this statement anywhere else right now and it would work, just not inside of the %NRSTR.

 

I've also tried UNQUOTE around the %goto bit but EGuide acts very strange - clearly it's causing a mismatch of some sorts and EGuide has no idea how to compile and run the entire macro.

 

 

Astounding
PROC Star

Unfortunately, next suggestion would be to follow the @RW9 advice and get rid of IFC.  There should be no problem using %goto in this context:


%else %do;
  %put ERROR: LIBNAME does not exist;

  %goto exit;
%end;

Tom
Super User Tom
Super User

Question seems silly. 

 

For %GOTO to work you need to be in a macro. If you are in a macro then why the heck are you using data step function IFC instead of just use %IF?

 

Are you trying to see if you can build a computed %GOTO?

 

 

_Dan_
Quartz | Level 8

Approach is 50% programmatic and 50% cosmetic. I've replaced it with a %let error = 1 and then I query this later. There's more than one of these statements so I just let them all run and now I query the outcome afterwards. %goto is now called by its own %if error = 1 %then...

s_lassen
Meteorite | Level 14

The %goto statement can only be used inside a macro. However, even inside a macro you will get the same message when trying to use %goto like that (because the statement has not been compiled as part of the macro). If you are inside a macro, there is ho need to make things so complicated, though, just use:

%if %sysfunc(libref(libname)) %then %do;
  %put ERROR: LIBNAME Libref does not exist.; 
%goto exit;
%end;
%else
%put NOTE: LIBNAME Libref exists.;

Except that unless you have some cleanup code that you want to execute at the %exit label, you could just use %return to stop macro execution.

_Dan_
Quartz | Level 8

Thanks for that, I'd been using %if %then %do for a long time but wanted to use IFC more after seeing (and using) some very good examples. Looks like that's just not possible when calling another macro function but everything else works without issue. Shame, it's the only time I ever needed to use %goto as well.

 

Cheers.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 9 replies
  • 1001 views
  • 0 likes
  • 5 in conversation