10-09-2017 11:26 AM
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.
-- my code --;
&custom_code; * This macro variable inserts custom code and sometimes have unbalanced quotes ;
-- my remaining code --;
10-09-2017 01:11 PM
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.
10-09-2017 01:15 PM
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
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;
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;
10-09-2017 01:17 PM
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.
10-09-2017 01:24 PM
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.
10-10-2017 12:32 AM
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.