BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
dipand
Quartz | Level 8

Hi all,

I have this macro SAS:


%macro aggiorna(id=);
proc sql;
select distinct id,LOCATION_DESCRIPTION1 into :id ,:location_description1
from lo1
where id=&id.;
quit;
%put valore id ------>&id.;
%put valore location_description ------>&location_description1.;

proc sql;
UPDATE a2.inspection SET location_description =%unquote("&LOCATION_DESCRIPTION1.") WHERE ID = &id.;
quit;
%mend;

proc sql;
create table LOCATION as
select distinct id
from lo1
;
quit;
data _null_;
set LOCATION(OBS=1);
call execute('%nrstr(%aggiorna(id='!!id!!'));');
run;

 

I must update the column in the dataset with the value that I have in my dataset, but che macrovariable &location_description1. contains value with apostrophe and I receive this error:

 

ERROR: Literal contains unmatched quote.

For example, value macro  &location_description1:

 

PANTANO D'AVIO'

 

 

and I don't find a solution.

How is possibile resolve my error?

 

I try use different function for example %nrquote or %bquote but I don't resolve.

 

Thank you for your response.

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

It is not clear what you are trying to do and why you are trying to store data into macro variables just so you can then move it back into data.

 

But the easiest solution is to add the actual quotes to the macro variable when it is created. Then you will not have problems using the macro variable value in code (so you won't need to use either the %BQUOTE() or  the %UNQUOTE() function). 

 

Pass a single quote (aka apostrophe) as the optional second argument to QUOTE() and the value will be quoted with single quotes so that any & or % characters will also be protected from being used by the macro processor.

proc sql noprint;
%let location_description1=' ';
select quote(trim(LOCATION_DESCRIPTION1),"'")
  into :location_description1
  from lo1
  where id=&id.
;
quit;

Then when you use the macro variable there is no need to add more quote characters.

UPDATE a2.inspection
 SET location_description = &LOCATION_DESCRIPTION1 
 WHERE ID = &id.
;

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

It is not clear what you are trying to do and why you are trying to store data into macro variables just so you can then move it back into data.

 

But the easiest solution is to add the actual quotes to the macro variable when it is created. Then you will not have problems using the macro variable value in code (so you won't need to use either the %BQUOTE() or  the %UNQUOTE() function). 

 

Pass a single quote (aka apostrophe) as the optional second argument to QUOTE() and the value will be quoted with single quotes so that any & or % characters will also be protected from being used by the macro processor.

proc sql noprint;
%let location_description1=' ';
select quote(trim(LOCATION_DESCRIPTION1),"'")
  into :location_description1
  from lo1
  where id=&id.
;
quit;

Then when you use the macro variable there is no need to add more quote characters.

UPDATE a2.inspection
 SET location_description = &LOCATION_DESCRIPTION1 
 WHERE ID = &id.
;

 

Quentin
Super User

Looks to me like that error message is coming from the %PUT statement:

%put valore location_description ------>&location_description1.;

Did you try:

%put valore location_description ------>%bquote(&location_description1.);

I can replicate the error message with:

1    %macro foo() ;
2    data _null_ ;
3      call symputx("foo","O'Henry") ;
4    run ;
5    %put &foo ;
6    %mend foo ;
7    %foo()
MPRINT(FOO):   data _null_ ;
MPRINT(FOO):   call symputx("foo","O'Henry") ;
MPRINT(FOO):   run ;


ERROR: Literal contains unmatched quote.
O'Henry'
ERROR: The macro FOO will stop executing.
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 3581 views
  • 0 likes
  • 3 in conversation