DATA Step, Macro, Functions and more

Catch unexpected quotes in external code and recover program

Reply
Contributor
Posts: 27

Catch unexpected quotes in external code and recover program

Hi,

I am stuck in a problem with unbalanced quotes and would greatly appreciate your help. I have a macro as shown below. Here I have an external piece of custom code pasted in via a macro variable (or %include statement). This custom code isn't tested upfront until it is used here. Sometimes this custom code has unbalanced quotes and causes my remaining code to fail. I have tried the strings such as *’; *”; */; or *); */; /*’*/ /*”*/; %mend; after the custom code, to catch such issues and continue with my remaining code normally. But nothing so far has been successful. Please help. 

 

Thanks

Vineet

 

%macro mymacro;

 

-- my code --;

 

&custom_code; * This macro variable inserts custom code and sometimes have unbalanced quotes ;

 

-- my remaining code --;

 

%mend mymacro; 

 

%mymacro;

Super User
Posts: 10,574

Re: Catch unexpected quotes in external code and recover program

Posted in reply to vineet7878_gmail_com

vineet7878_gmail_com wrote:

This custom code isn't tested upfront until it is used here.


Shoot the one who gives you untested code. With a field howitzer. 3 hours.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
SAS Super FREQ
Posts: 4,275

Re: Catch unexpected quotes in external code and recover program

Posted in reply to vineet7878_gmail_com

I suggest you insert a sanity check for basic syntax before starting the computation. You can use a DATA step to read the custom code as a series of strings and perform basic syntax checking such as counting the number of opening/closing parentheses. You can abort the computation if the &custom_code is invalid:

 

For example, suppose you take the custom string and write it to a temporary file.  The following example assumes that you write it to 

C:/temp/temp.sas

and that the file contains the following text:

 

if (f>0) and (f<1 then do;
s = "is this string closed?";
t = "My name is;
x = log(t;
end;

 

Now you can parse that text file, as follows:

/* check open/close parentheses and balanced quotes */
data _null_;
infile 'C:/temp/temp.sas' end=eof;
input;
netParen + countc(_infile_, '(') 
         - countc(_infile_, ')');
sumQuotes + countc(_infile_, '"');
if eof then do;
	balancedQuotes = mod(sumQuotes+1, 2);
   put netParen=;        /* if 0 then parens balanced */
   put balancedQuotes=;  /* if 0, then quotes vbalanced */
end;
run;

 

Super User
Posts: 10,574

Re: Catch unexpected quotes in external code and recover program

Posted in reply to vineet7878_gmail_com

My previous post should show you that there is no remedy. An option is to copy the "magic code" that Enterprise Guide sends after every code submitted, but for every idiot-proof solution devised, a new, improved idiot arises to overcome it.

 

The best solution is to not have code in macro variables (or simply entered as input); instead have parameters set, which you can test for validity, which then influence code already written.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 27

Re: Catch unexpected quotes in external code and recover program

Posted in reply to vineet7878_gmail_com

I like Ricks solution. The custom code is coming from SAS code entered as snippets within excel based data specifications file, from where it is exported to SAS to run. 

 

I guess, I need to build code testing module to test such code, before actually running it. I was hoping to not do such checking and just have some magic string at the end of code to catch the issues and continue without disruption.

Super User
Super User
Posts: 8,279

Re: Catch unexpected quotes in external code and recover program

Posted in reply to vineet7878_gmail_com

It is probably best to run each block of the untested code in its own SAS environment.  That way when it fails it does not fail the master job.

 

So perhaps for each snippet of code generate a stand alone program that tests it and run it as a separate SAS job. Then scan the log for a success code before attempting to use the code.

Ask a Question
Discussion stats
  • 5 replies
  • 118 views
  • 4 likes
  • 4 in conversation