BookmarkSubscribeRSS Feed
Wolverine
Pyrite | Level 9

I'm working with a large and somewhat complex data collection, and the code uses 2 and sometimes 3 nested macros. Often, I will see an error in the log, so I'll press the Cancel button to stop the code from running. Then I correct the error and rerun the code. But I often get an error about a quoted string being longer than 256 characters (which the administrators of the virtual environment I work in tell me is related to a macro that didn't end properly). Or it will start running from the point where the previous run was in the macro (ie, the 3rd file of the 2nd year, rather than the 1st file of the 1st year).

 

My only solution to these errors has been to close SAS EG, re-open it (which require logging in again), wait for it to access the server, rerun all my libname statements and global macro variables (ie, %let version=2;), open the code I want to run, and then rerun it. This is tedious and time-consuming.

 

Isn't there a way to get the macro to stop running so I can start a new run of the code? I've tried typing %mend; quit; and %abort; and then running one or all of those commands, but they just generate errors. It seems like the Cancel button should shut down any running macros, but it doesn't.

 

To help recreate this issue, try running the following code from %macro to run;. Then run the full code. When I do this, i get an error about a quoted string and the files test_t1 and test_t2 are not created.

%macro example (ex);

data test_&ex.; set sashelp.cars; run;

%mend;
%example(t1)
%example(t2)
10 REPLIES 10
ChrisHemedinger
Community Manager

I think it's possible that the "quote string" issue is a red herring here. That warning can come up in a number of legitimate situations. Sometimes it's noise, and you can suppress it with:

options noquotelenmax;

 

If running your macros but not letting them finish (interrupting them) leaves the session in an uncertain state, you can "reset" your SAS session by right-clicking on the SAS server name in the Servers view, then select Disconnect. It's a little less drastic than shutting down EG. You can then run your code again, and it will connect to a fresh SAS session.

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
Wolverine
Pyrite | Level 9

@ChrisHemedinger wrote:

I think it's possible that the "quote string" issue is a red herring here. That warning can come up in a number of legitimate situations. Sometimes it's noise, and you can suppress it with:

options noquotelenmax;

 

If running your macros but not letting them finish (interrupting them) leaves the session in an uncertain state, you can "reset" your SAS session by right-clicking on the SAS server name in the Servers view, then select Disconnect. It's a little less drastic than shutting down EG. You can then run your code again, and it will connect to a fresh SAS session.


To provide some more information, the installation of SAS EG that I'm using includes some background style code that is run automatically whenever a user submits code. Normally this background code is invisible, but this interrupted macro situation makes it appear in the log. When I run the macro I provide in the manner I described, a line of code shows up as an error (Statement is not valid or is used out of proper order): ;*';*";*/;quit/run;  Some of the other lines in this background code include quotes, and I think that has something to do with the quoted string issue I see sometimes.

 

Anyway, I tried disconnecting and reconnecting to the server, but I'm still getting the same errors.

ChrisHemedinger
Community Manager

When you connect to a server with EG, EG does submit some boilerplate code / utility macros that are commonly used in EG projects. In addition, a user or an admin can specify additional code to run (controlled in Tools->Options->SAS Programs).

 

If you want to see the entire set of code that's run at server start, you can get to it in the Server properties window, "Software" tab. This (old but still accurate) blog post shows you how to see it.

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
Quentin
Super User

@Wolverine wrote:

To help recreate this issue, try running the following code from %macro to run;. Then run the full code. When I do this, i get an error about a quoted string and the files test_t1 and test_t2 are not created.

%macro example (ex);

data test_&ex.; set sashelp.cars; run;

%mend;
%example(t1)
%example(t2)

I can replicate the problem with your example, but I'm not sure the example really relates to a problem caused by pressing the CANCEL button while a macro is executing.

 

As you say, EG adds a magic string after every submission:

;*';*";*/;quit;run;

The intention of that string is to close any unclosed quotation blocks, or comment blocks. 

 

But if you submit just part of the macro definition, without the %MEND, then when EG adds the magic string it will be like you submitted:

%macro example (ex);

data test_&ex.; set sashelp.cars; run;

;*';*";*/;quit;run;

In that case, you will have an unclosed macro definition, but you will also have an unclosed block of single quotes.  This happens because * is not a comment in the macro language, so the quotation mark will be seen by the macro processor.

See this related thread:
https://communities.sas.com/t5/SAS-Programming/Apostrophe-in-a-comment-in-a-macro-why-after-40-years...

 

Personally, I really like that EG adds the magic string, but I understand why it was done.  I wonder if it would have been safer to use a string like:

;%*';%*";*/;quit;run;

The %* comment would hide the unmatched quotes whether it appeared in a macro definition or in open code.

Wolverine
Pyrite | Level 9

@Quentin wrote:

I can replicate the problem with your example, but I'm not sure the example really relates to a problem caused by pressing the CANCEL button while a macro is executing.

 

As you say, EG adds a magic string after every submission:

;*';*";*/;quit;run;

The intention of that string is to close any unclosed quotation blocks, or comment blocks. 

 

But if you submit just part of the macro definition, without the %MEND, then when EG adds the magic string it will be like you submitted: 

%macro example (ex);

data test_&ex.; set sashelp.cars; run;

;*';*";*/;quit;run;

In that case, you will have an unclosed macro definition, but you will also have an unclosed block of single quotes.  This happens because * is not a comment in the macro language, so the quotation mark will be seen by the macro processor.


Well obviously the code I provided isn't nearly as complicated as the code I'm actually running. When I hit Cancel, it seems like it stops processing where ever it is in the code and therefore doesn't necessarily reach the %mend statement. That's what I was trying to duplicate with my example.

 

In any event, the unclosed block of single quotes issue is still due to the macro not completing. Whether it doesn't complete because there is no %mend statement or because the code submission was canceled before it reached the %mend statement, the result is the same: I can't rerun my code successfully without opening a new instance of SAS EG. So how do I stop the unclosed macro from continuing to run? It seems like submitting a %mend statement should do it, but that doesn't work.

Quentin
Super User

Where I'm stuck in my thinking is that the problem as I think about it should only happen if you hit cancel while a macro is being compiled.  Hitting cancel while a macro is executing shouldn't cause the problem.

 

In the thread I linked to there is an answer from jimbarbour which solved the problem, but I couldn't get it to work just playing with it a bit.  When I gut stuck in an umatched quote loop in EG, I typically resort to the server disconnect and reconnect method that Chris mentoioned.

Wolverine
Pyrite | Level 9

@Quentin wrote:

Where I'm stuck in my thinking is that the problem as I think about it should only happen if you hit cancel while a macro is being compiled.  Hitting cancel while a macro is executing shouldn't cause the problem.


I just tested that... I opened a new instance of SAS EG and submitted the full example code. I let it run until it began the first data step (about 8 seconds) and then hit cancel. Then I tried to run the full example code again, and I got the same error as when I ran it without the %mend statement and then tried to run the full code.

Quentin
Super User

@Wolverine wrote:

@Quentin wrote:

Where I'm stuck in my thinking is that the problem as I think about it should only happen if you hit cancel while a macro is being compiled.  Hitting cancel while a macro is executing shouldn't cause the problem.


I just tested that... I opened a new instance of SAS EG and submitted the full example code. I let it run until it began the first data step (about 8 seconds) and then hit cancel. Then I tried to run the full example code again, and I got the same error as when I ran it without the %mend statement and then tried to run the full code.


I tried replicating  it by running your code with a SLEEP function added, but could not.

 

%macro example (ex);

data test_&ex.; 
   set sashelp.class;
   Q=sleep(1,1) ;
run;

%mend;
%example(t1)
%example(t2)

When I hit cancel the log shows it canceled, and the output data set is incomplete.  But then when I run the whole program, it runs fine.  So canceling in the middle of macro execution didn't cause unmatched quotes.

Are you able to replicate the problem with something like above?

Tom
Super User Tom
Super User

How is your code structured?

Are you using the autocall facility to define your macros?

Or do you have the macro definitions and plain SAS code mixed together like in your example?

 

It might make it work better if you define all of the macros first.  Then put the plain SAS code that calls them last.  Then there would be much less chance that cancelling the submission would interrupt the compilation of the macros.

Ksharp
Super User
If your SAS was installed under a server side, can you ask SAS Administrator to reboot the server/computer ?

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
  • 10 replies
  • 180 views
  • 1 like
  • 5 in conversation