Date: Tue, 5 Apr 2005 09:22:16 -0400
Reply-To: "Richard A. DeVenezia" <radevenz@IX.NETCOM.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Richard A. DeVenezia" <radevenz@IX.NETCOM.COM>
Subject: Templates, regular expressions, resolutions and callbacks
Content-Type: text/plain; charset="iso-8859-1"
I have some template html I want to process through a data step.
filename template temp;
data _null_;
file template;
input;
put _infile_;
datalines4;
<html>
<head>
<title><macro>&title</macro></title>
</head>
<body>
<h1><macro>&foo</macro> and <macro>&bar</macro></h1>
<p>An ampersand looks like this: &</p>
</body>
</html>
;;;;
In the template, anything within <macro>...</macro> is to be resolved by the
macro system and replaced into the template. The fake tags are used because
normal html can have lots of & and % that should not be damaged by blindly
using RESOLVE.
So, I need a data step that reads the template and does the replacement as
needed.
*if* SAS had replacement with *callback* (in the manner of PHP's
preg_replace_callback) everything could be done neatly as follows:
%let title = This is my Title;
%let foo = Cats;
%let bar = Dogs;
filename resolved temp;
data _null_;
infile template;
file resolved;
if _n_ = 1 then
rx = prxParse ('s:<macro>(.*?)</macro>:`resolve($1)`:io');
retain rx;
input;
if prxMatch (rx, _infile_) then do;
call prxChange (rx,-1,_infile_);
putlog _infile_;
end;
put _infile_;
run;
What is callback? Look at the replacement text carefully, it contains
backticks (also known as grave). If a replace contains backticks, the
system would presume that which is enclosed in a function call that has to
be made, whose results are used as the replacement text.
A look at the log shows
------
<title>`resolve(&title)`</title>
<h1>`resolve(&foo)` and `resolve(&bar)`</h1>
------
So, alas, SAS prx* definitely does not (and may never) support callbacks.
Callbacks would be a great benefit it they were ever added to the prx*
repertoire. [call prxChangeWithCallback]
Any ideas on how to replace (possibly multiple times) the <macro> tag and
its encapsulated expression, with the expression's resolution per the macro
system?
Richard A. DeVenezia