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 is hosting free webinars!
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now at https://www.basug.org/events.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 5023 views
  • 0 likes
  • 3 in conversation