Date: Thu, 6 Jan 2011 17:08:33 +0000
Reply-To: "Fehd, Ronald J. (CDC/OCOO/ITSO)" <rjf2@CDC.GOV>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Fehd, Ronald J. (CDC/OCOO/ITSO)" <rjf2@CDC.GOV>
Subject: Re: Macro Quoting Issue (I think) - Problem with CALL EXECUTE
parameter values with Apostrophe, Comma
In-Reply-To: <456B52C41B724C41B96561D7AD283E7D01F0503F@mail.chpdm.umbc.edu>
Content-Type: text/plain; charset="us-ascii"
summary:
1. in calling routine, remove special characters in parameters
2. expand in the subroutine
i.e.
1. change QUEEN ANNE'S to QUEEN ANNE!S
2. change QUEEN ANNE!S back to QUEEN ANNE'S
see other tricks in this paper:
List Processing Basics Creating and Using Lists of Macro Variables
Tiny Url: http://tinyurl.com/5p3j2z for this page
http://tinyurl.com/6mmqpj for paper
http://www2.sas.com/proceedings/forum2007/113-2007.pdf
SGF 2007, paper 113, section: Hands On Workshops
List Processing Basics: Creating and Using Lists of Macro Variables
* Ronald J. Fehd
* Art Carpenter
Ron Fehd the macro maven
> -----Original Message-----
> From: owner-sas-l@listserv.uga.edu [mailto:owner-sas-
> l@listserv.uga.edu] On Behalf Of Jack Clark
> Sent: Thursday, January 06, 2011 10:17 AM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Macro Quoting Issue (I think) - Problem with CALL EXECUTE
> parameter values with Apostrophe, Comma
>
> Hello and Happy New Year,
>
>
>
> This is a somewhat long post, but I have tried to clearly provide as
> much information as possible. I need some help with what I think may
> be
> a macro quoting issue. I have tried to simplify the code for this
> post.
> I am developing a SAS program which will select records from the source
> data based on COUNTY and generate a report.
>
>
>
> A couple of notes
>
> - Some COUNTY names in the source data contain parentheses and
> apostrophes
>
> - Some COUNTYs have multiple values in the source data, so all
> must be selected when running for that county
>
>
>
> I have successfully written logic which allows me to select 1 county
> and
> the report is generated for that county (see below):
>
>
>
> * 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;
>
>
>
>
>
>
>
> Now I have to add the ability for the program to generate reports by
> COUNTY for multiple counties (or ALL counties). My initial strategy
> was
> to wrap the reporting code in macro, generate a data set that contains
> one record per COUNTY to be run (with the parameters needed) and then
> use CALL EXECUTE to generate the reporting for each COUNTY. I added
> the ALL category to the format, so user can select to run ALL counties.
>
>
>
>
> The code below works when CARROLL is selected as the COUNTY, but fails
> for BALTIMORE and QUEEN ANNE'S. I believe BALTIMORE is failing because
> of the comma "," in the value of CLIST when the CALL EXECUTE runs. SAS
> is seeing the comma as separating macro parameters in the CTYRPT macro
> instead of part of the CLIST2 parameter. I believe QUEEN ANNE'S is
> failing because of the apostrophe. I have tried %bquote around COUNTYT
> in the CALL EXECUTE (and many other variations), but can't seem to get
> it.
>
>
>
> If someone can tell me how to correct the process below, I would
> appreciate it. Or, if there are suggestions for a better approach I am
> open to those as well. Thanks in advance.
>
>
>
>
>
>
>
> ** method #2 - build a data set to use with CALL EXECUTE to run report
> for multiple counties ;
>
> %let ccode = 03;
>
>
>
> 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;
>
> call execute('%ctyrpt(countyt2='||countyt||',clist2='||clist||')');
>
> run;
>
>
>
>
>
> ******* log when I run for COUNTY name with apostrophe....
>
>
>
> 472
>
> 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.
>
> NOTE: The SAS System stopped processing this step because of errors.
>
> NOTE: There were 1 observations read from the data set WORK.DRIVER.
>
> NOTE: DATA statement used (Total process time):
>
> real time 0.00 seconds
>
> cpu time 0.00 seconds
>
>
>
>
>
> NOTE: CALL EXECUTE routine executed successfully, but no SAS statements
> were generated.
>
>
>
>
>
> ******* log when I run for BALTIMORE - has multiple values in CLIST and
> contains parentheses
>
> ******* I know this message is about the COMMA in the CLIST variable,
> but don't know how to fix it
>
> 508
>
> 509 data _null_;
>
> 510 set driver;
>
> 511 call
> execute('%ctyrpt(countyt2='||countyt||',clist2='||clist||')');
>
> 512 *call execute('%cty(countyt2='||%bquote(countyt)||')');
>
> 513 run;
>
>
>
> ERROR: All positional parameters must precede keyword parameters.
>
> NOTE: The SAS System stopped processing this step because of errors.
>
> NOTE: There were 1 observations read from the data set WORK.DRIVER.
>
> NOTE: DATA statement used (Total process time):
>
> real time 0.00 seconds
>
> cpu time 0.00 seconds
>
>
>
>
>
> NOTE: CALL EXECUTE routine executed successfully, but no SAS statements
> were generated.
>
>
>
>
>
> Jack Clark
> Senior Programmer
> phone: 410-455-6256
> fax: 410-455-6850
> jclark@hilltop.umbc.edu
>
> University of Maryland, Baltimore County
> Sondheim Hall, 3rd Floor
> 1000 Hilltop Circle
> Baltimore, MD 21250
>
> Please consider the environment before printing this e-mail and/or any
> attachments.
>
>
> Confidentiality Notice: This e-mail may contain information that is
> legally privileged and that is intended only for the use of the
> addressee(s) named above. If you are not the intended recipient, you
> are hereby notified that any disclosure, copying of this e-mail,
> distribution, or action taken in reliance on the contents of this e-
> mail and/or documents attributed to this e-mail is strictly prohibited.
> If you have received this information in error, please notify the
> sender immediately by phone and delete this entire e-mail. Thank you.
|