BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have the following SAS code which works on its own (no macro)

ods results on;
ods rtf
body="C:\test.rtf";
ods rtf text = '~R/RTF" \pard \phmrg\posxc \qc\fs36\b " What a Great Title ~R/RTF" \par "' ;
proc print data=sashelp.class;
run;
ods rtf close;

I would like to be able to parameterize the rtf string so that a user could control alignment, bolding, fontsize, etc. In the macro I created, the string that gets created either causes an error like:

MPRINT(INSERTTEXT): ods rtf text = '~R/RTF" \pard \phmrg\posxc \qc\fs36\b " What a Great Title ~R/RTF" \par "';

ERROR 22-322: Expecting a quoted string.
ERROR 200-322: The symbol is not recognized and will be ignored.

This is especially frustrating since the resolved macro variable is exactly what I manually entered in the ods rtf text statement above. So what's the difference between executing it as SAS code and letting the macro processor resolve it into SAS and then having SAS executing it? It's the same thing right? (clearly it's not 😉

Here's a simplified version of the macro:

%macro insertText(text, justify, fontSize, bold);
ods escapechar="~";

%let startRtf = %str(%'~R/RTF%" \pard \phmrg\posxc );
%let fontConfig = %str(\q&justify.\fs&fontSize.\&bold %" );
%let endRtf = %str(~R/RTF%" \par %"%');
%let wholeString = %str(ods rtf text = &startRtf&fontConfig%str( &text )&endRtf;);
%str(&wholeString);
%mend insertText;
ods rtf
body="C:\test.rtf";
%insertText(What a Great Title, c, 36, b)
proc print data=sashelp.class;
run;
ods rtf close;

MPRINT(INSERTTEXT): ods rtf text = '~R/RTF" \pard \phmrg\posxc \qc\fs36\b " What a Great Title ~R/RTF" \par "';

ERROR 22-322: Expecting a quoted string.
ERROR 200-322: The symbol is not recognized and will be ignored.

I imagine that my problem lies somewhere in the macro quoting, but more importantly, I'd like to understand why the resolved macro string throws an error when executed in the ODS RTF while the "regular" SAS code does not.

I should have mentioned at the top, I know there are easier ways to enter a title with formatting (~S={}, justify=left, etc.), but I need the user to be able to enter this anywhere in an RTF document, not just the title.

TIA,
Dave
5 REPLIES 5
Andre
Obsidian | Level 7
Dave,

I have test this solution with the use of bquote avoiding the % troubling use for isolated ' and so on
but i fall also upon the same error message

[pre]
191 ods rtf text=%insertText(What a Great Title, c, 36, b)
NOTE: Line generated by the macro variable "RTF".
1 '~R/RTF"\pard \phmrg\posxc \qc\fs36\b " What a Great Title ~R/RTF"\par "'
-
22
-
200
MPRINT(INSERTTEXT): '~R/RTF"\pard \phmrg\posxc \qc\fs36\b " What a Great Title ~R/RTF"\par
"';
ERROR 22-322: Expecting a quoted string.
ERROR 200-322: The symbol is not recognized and will be ignored.
192 proc print data=sashelp.class;
193 run;
[/pre]

With a unquote the quoted string is founded!

[pre]
ods escapechar="~";
options mprint;
%macro insertText(text, justify, fontSize, bold);
%let Rtf = %bquote('~R/RTF"\pard \phmrg\posxc \q&justify.\fs&fontSize.\&bold "
%str( &text ) ~R/RTF"\par "');

%unquote(&Rtf.);
%mend insertText;

ods rtf
body="d:\temp\test.rtf";
ods rtf text=%insertText(What a Great Title, c, 36, b)
proc print data=sashelp.class;
run;
ods rtf close;

[/pre]

But i have also a question now
because with this variation i cann't explain the appearance of the ; in the title!
Anybody ? Cynthia?
Quote handling in macro is always delicate!

[pre]
ods escapechar="~";
options mprint;
%macro insertT(text, justify, fontSize, bold);
%let Rtf = %bquote('~R/RTF"\pard \phmrg\posxc \q&justify.\fs&fontSize.\&bold ");
%let Rtf1=%bquote( %str( &text ) ~R/RTF"\par "');
%unquote(&Rtf.&Rtf1.);
%mend insertT;


ods rtf
body="d:\temp\test.rtf";
ods rtf text=%insertT(What a Great Title, c, 36, b)
proc print data=sashelp.class;
run;
ods rtf close;
[/pre]
PatrickG
SAS Employee
Try this, it seems to work. Using an %unquote instead of %str removes all of the masking of special characters (altered by the original %str( )s) during execution, which I suspect was the original problem.

If you want to write that line to the SAS log, I suppose you can put a %put in there as well...

%macro insertText(text, justify, fontSize, bold);
ods escapechar="~";

%let startRtf = %str(%'~R/RTF%" \pard \phmrg\posxc );
%let fontConfig = %str(\q&justify.\fs&fontSize.\&bold %" );
%let endRtf = %str(~R/RTF%" \par %"%');
%let wholeString = %str(ods rtf text = &startRtf&fontConfig%str( &text )&endRtf;);
%unquote(&wholeString);
%mend insertText;
ods rtf
body="C:\test.rtf";
%insertText(What a Great Title, c, 36, b)
proc print data=sashelp.class;
run;
ods rtf close;
PatrickG
SAS Employee
Meant to add that when I use %unquote() I don't see the semicolon in the title that Andre mentioned.
Andre
Obsidian | Level 7
Yes,

I ommit to report here that there has been a long discussion about this on sas-l
and that the answer of Ian Withlock was very deep upon
the way why (despite the fact that %str is better) with %bquote it was working
and specially why a ; was appearing.

http://groups.google.fr/group/comp.soft-sys.sas/browse_thread/thread/17a2da7078b7c0b9#
Duong
Obsidian | Level 7
Hi All

This is a solution that I posted to the individuals a few days ago.

%macro test(text=);
ods rtf body="test.rtf";
ods rtf text = "{&text}";
proc print data=sashelp.class;
run;
ods rtf close;
%mend;

%test(text={\pard \phmrg\posxc {\qc\fs36\b\cf2 What} {\super a Great} > Title \par});


This paper will explain more details.
http://www.lexjansen.com/phuse/2007/po/po06.pdf



Regards
Duong

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 1786 views
  • 0 likes
  • 4 in conversation