Date: Thu, 6 Jan 2011 14:52:40 -0500
Reply-To: Chang Chung <chang_y_chung@HOTMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Chang Chung <chang_y_chung@HOTMAIL.COM>
Subject: Re: Macro Quoting Issue (I think) - Problem with CALL EXECUTE
parameter values with Apostrophe, Comma
On Thu, 6 Jan 2011 10:17:27 -0500, Jack Clark <jclark@HILLTOP.UMBC.EDU>
wrote:
...
>******* log when I run for COUNTY name with apostrophe....
>473 data _null_;
>474 set driver;
>475 call execute('%ctyrpt(countyt2='||countyt||',clist2='||clist||')');
>476 run;
>
>NOTE 49-169: The meaning of an identifier after a quoted string may
>change in a future SAS
> release. Inserting white space between a quoted string and
>the succeeding
> identifier is recommended.
>
>ERROR: Macro parameter contains syntax error.
...
Hi, Jack:
A nice posting! I appreciate the simplified problem, test data, and log and
error messages. The solution is simply macro quoting a bit more properly --
including masking the commas and unmatched single quotes. You only need to
modify the last data _null_ step. I have included the lst output when the
ccode is set to 99. HTH.
Cheers,
Chang
* source data ;
data test (drop=i);
infile cards missover;
input @01 countyname $16.;
do i = 1 to 5;
output;
end;
cards;
BALTIMORE
BALTIMORE (CITY)
CARROLL
QUEEN ANNE'S
;
run;
proc format;
value $ cty
'01' = "CARROLL"
'02' = "BALTIMORE"
'03' = "QUEEN ANNE'S"
;
run;
** method #1 - use CALL SYMPUT to load macro variables and run report
for single county ;
%let ccode = 03;
data _null_;
countyt = put("&ccode.",$cty.);
if "&ccode." = "02" then clist = "'BALTIMORE','BALTIMORE (CITY)'";
else clist = quote(trim(countyt));
call symput('countyt1',countyt);
call symput('clist1',clist);
run;
title1 "METHOD #1 - Records from TEST Where COUNTYNAME = %bquote
(&countyt1.)";
proc print data = test;
where countyname in (&clist1.);
run;
** method #2 - build a data set to use with CALL EXECUTE to run report
for multiple counties ;
%let ccode = 99;
proc format;
value $ cty
'01' = "CARROLL"
'02' = "BALTIMORE"
'03' = "QUEEN ANNE'S"
'99' = "ALL"
;
run;
data driver;
length countyt $25 clist $50;
if "&ccode." in ("01","99") then do;
countyt = "CARROLL";
clist = quote(trim(countyt));
output;
end;
if "&ccode." in ("02","99") then do;
countyt = "BALTIMORE";
clist = "'BALTIMORE','BALTIMORE (CITY)'";
output;
end;
if "&ccode." in ("03","99") then do;
countyt = "QUEEN ANNE'S";
clist = quote(trim(countyt));
output;
end;
run;
%macro ctyrpt (countyt2=,clist2=);
title1 "METHOD #2 - Records from TEST Where COUNTYNAME = %bquote
(&countyt2.)";
proc print data = test;
where countyname in (&clist2.);
run;
%mend ctyrpt;
data _null_;
set driver;
* properly macro quote the arguments;
length temp arg1 arg2 $32767;
temp = '%ctyrpt(countyt2=#1#,clist2=#2#)';
arg1 = tranwrd(countyt, '''', '%''');
arg1 = catt('%str(', arg1, ')');
temp = tranwrd(temp, '#1#', trim(arg1));
arg2 = tranwrd(clist, '''', '%''');
arg2 = catt('%str(', arg2, ')');
temp = tranwrd(temp, "#2#", trim(arg2));
* do call;
call execute(trim(temp));
run;
%put ccode=&ccode;
/* on log
ccode=99
*/
/* on lst -- in part
METHOD #2 - Records from TEST Where COUNTYNAME = CARROLL
Obs countyname
11 CARROLL
12 CARROLL
13 CARROLL
14 CARROLL
15 CARROLL
METHOD #2 - Records from TEST Where COUNTYNAME = BALTIMORE
Obs countyname
1 BALTIMORE
2 BALTIMORE
3 BALTIMORE
4 BALTIMORE
5 BALTIMORE
6 BALTIMORE (CITY)
7 BALTIMORE (CITY)
8 BALTIMORE (CITY)
9 BALTIMORE (CITY)
10 BALTIMORE (CITY)
METHOD #2 - Records from TEST Where COUNTYNAME = QUEEN ANNE'S
Obs countyname
16 QUEEN ANNE'S
17 QUEEN ANNE'S
18 QUEEN ANNE'S
19 QUEEN ANNE'S
20 QUEEN ANNE'S
*/