| Date: | Thu, 5 Jul 2007 11:50:41 -0500 |
| Reply-To: | Kevin Morgan <kmorgan@GRAINSCANADA.GC.CA> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Kevin Morgan <kmorgan@GRAINSCANADA.GC.CA> |
| Subject: | Re: Macro Quoting Question |
|
| In-Reply-To: | <16FD64291482A34F995D2AF14A5C932C015A7567@MAIL002.prod.ds.russell.com> |
| Content-Type: | text/plain; charset="iso-8859-1" |
Hi Mark:
Thanks very much for the response.
As soon as I saw the triple ampersand in the solution that yourself, Toby, and Ovidiu had sent I realized my error. I have recently tried to read through chapter two, the Mechanics of Macro processing of Michele Burlew's book SAS Macro Programming Made Easy. To be honest with you the only thing that stuck with me was the term "Tokenizer" and Token the rest went over my head or was forgotten.
I guess what I failed to understand was that the ampersands were related to the number of "loops" a macro variable makes through a macro processer. Thanks very much for clarifying this for me. Now, for some lunchtime reading I am going to go back to chapter 2 of Michele Burlew's book and try to understand the Macro processer a little better.
Again Thank You,
Kevin
Kevin Morgan | (204) 983-4070 | <mailto: kmorgan@grainscanada.gc.ca>
Grain Statistics / Statistique
facsimile / télécopieur (204) 983-7550 |
Corporate Services / Services à l'organisme |
Canadian Grain Commission | 900-303 Main Street,
Commission canadienne des grains | 303, rue Main, pièce 900 |
Winnipeg MB R3C 3G8
-----Original Message-----
From: Terjeson, Mark [mailto:Mterjeson@russell.com]
Sent: Thursday, July 05, 2007 9:52 AM
To: Kevin Morgan; SAS-L@LISTSERV.UGA.EDU
Subject: RE: Macro Quoting Question
Hi Kevin,
Macro quoting can be a challenge at times,
however I also have to say that needing
macro quoting is usually pretty rare for
the times that people think they need to
resort to macro quoting.
As many know, I like to promote SAS macro
stuff as "text substitution" here on SAS-L
and in user group papers. I suggest that
mentally realizing that the macro processor
occurs before the runtime compiler, the
macro pre-parser passes through the code
as many times as it needs to resolve the
macro code "text" before it gets sent to
the runtime for execution.
I find it is easier the write and troubleshoot
macro code think of it in this way. In doing
so, each pass through the text resolves the
% and & macro symbols, so if you have a text
string that needs to resolve a portion of
the string first, essentially every other &
is going to get gobbled up. So if you need
the suffix of the string resolved first and
then that portion is going to be used in the
next pass as a macro variable name, then you
need double && ampersands because you need
to have ampersands in just the right place
on the second and third pass. i.e. double
&& ampersands on the first pass resolve to
a single & ampersand. Thus if there is
going to be a need for the & to be there
at the end, in order to resolve a macro
variable name after several passes build
the name itself, then you need enough
ampersands to be there at the end. You really
do not need quoting functions. In thinking
of SAS macro goodies as text substitution
and mentally thinking through the 1st pass,
substituting what the macro parser would
resolve, then pretend that result is going
to go through the macro parser again, then
picturing those results, and repeating until
all macro goodies are resolved, you can
actually walk through editing and changing
a sample play copy of your text just as the
parser is going to do and invariably you can
get it right the first time, and without any
quoting functions for the most part.
See working solution below:
%let S1 = Prairie; %let S2 = PrairieS;
%let S3 = LH ; %let S4 = LHS ;
%let S5 = VC ; %let S6 = VCS ;
%let S7 = VR ; %let S8 = VRS ;
%let S9 = NH ; %let S10 = NHS ;
%let S11 = EA ; %let S12 = EAS ;
%let S13 = BP ; %let S14 = BPS ;
%let S15 = OTHER ;
%let S16 = S_CG; %let S17 = SS_CG;
%let S18 = S_CH; %let S19 = SS_CH;
%let S20 = S_MJ; %let S21 = SS_MJ;
%let S22 = S_SK; %let S23 = SS_SK;
%let S24 = S_VC; %let S25 = SS_VC;
%let S26 = S_WG; %let S27 = SS_WG;
%let S28 = S_OTH;
%macro make_sample_data;
%do I=1 %to 28;
%put "Processing data set &&s&I.";
data &&S&I.;
do i = 1 to int(ranuni(0)*100);
output;
end;
run;
%end;
%mend;
%make_sample_data;
Options mprint;
%macro Summ;
%do I=1 %to 27;
%put "Processing data set &&s&I.";
%put "New mvar name is >S_&&s&I.<";
proc sql noprint;
select count(*) as count into: S_&&s&I. from &&S&I.;
quit;
%put "Macro S_&&s&I. has &&&&S_&&s&I. entries";
%end;
%mend summ;
%summ;
http://www.pnwsug.org/Conference_2006/Procedings/PNWSUGotherfiles/PN21Ma
rkTerjesonMacro101.pdf
Hope this is helpful.
Mark Terjeson
Senior Programmer Analyst, IM&R
Russell Investments
Russell Investments
Global Leaders in Multi-Manager Investing
-snip-
|