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.
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.
;
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.
;
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.
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!
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.