BookmarkSubscribeRSS Feed
vineet7878_gmail_com
Obsidian | Level 7

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;

5 REPLIES 5
Rick_SAS
SAS Super FREQ

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;

 

Kurt_Bremser
Super User

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.

vineet7878_gmail_com
Obsidian | Level 7

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.

Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 859 views
  • 4 likes
  • 4 in conversation