The SAS Output Delivery System and reporting techniques

ODS TEXT with macro variable containing %-sign inside

Accepted Solution Solved
Reply
Contributor
Posts: 59
Accepted Solution

ODS TEXT with macro variable containing %-sign inside

[ Edited ]

Once again I have struggled with an issue for ages, and could find an exact answer anywhere.

So my problem is that I need to use a macro variable inside ODS TEXT statement. The unfortunate part is that the macro variable could contain %-sign and it causes whole lot of issues. I have tried literally every trick I know and beyound.

Everything is fine when I use %SUPERQ with the macro variable, but when I do that in ODS TEXT it has no effect. I have attached a small example what I have trying to achieve.

data testing;
text='Some text %and some more';
call symput('mvar',text);
run;

%*Works like a charm;
%put %superq(mvar);

%*None of these work;
ods html;
ods text="<p class=""bold"">%superq(mvar)</p>";

%*This is not even possible?;
/*ods text=%nrbquote("<p class=""bold"">%superq(mvar)</p>");*/

ods text=%nrstr("<p class=""bold"">%superq(mvar)</p>");

ods text="<p class=""bold"">""&mvar.""</p>";

ods html close;

Accepted Solutions
Solution
‎02-02-2017 07:23 AM
SAS Super FREQ
Posts: 676

Re: ODS TEXT with macro variable containing %-sign inside

Hi

 

I think this is not really  SAS macro related, but rather the %A is somehow used by HTML.

 

You should change the % sign to the HTML Entity representation.

 

See here an example:

 

data testing;
  length text $ 1024;
  text='Some text %sugus some more';
  call symputx('mvar1',text);
  text='Some text %and some more';
  call symputx('mvar2',text);
  text='Some text &#37;and some more';
  call symputx('mvar3',text);
run;

%put %superq(mvar1);
%put %superq(mvar2);
%put %superq(mvar3);

ods html file="c:\temp\sometext.html";

ods text="<p class=""bold"">%superq(mvar1)</p>";
ods text="<p class=""bold"">%superq(mvar2)</p>";
ods text="<p class=""bold"">%superq(mvar3)</p>";

proc print data=sashelp.class(obs=1);
run;

ods html close;

You can find a list of the HTML Entities here https://dev.w3.org/html5/html-author/charref

 

Bruno

View solution in original post


All Replies
Solution
‎02-02-2017 07:23 AM
SAS Super FREQ
Posts: 676

Re: ODS TEXT with macro variable containing %-sign inside

Hi

 

I think this is not really  SAS macro related, but rather the %A is somehow used by HTML.

 

You should change the % sign to the HTML Entity representation.

 

See here an example:

 

data testing;
  length text $ 1024;
  text='Some text %sugus some more';
  call symputx('mvar1',text);
  text='Some text %and some more';
  call symputx('mvar2',text);
  text='Some text &#37;and some more';
  call symputx('mvar3',text);
run;

%put %superq(mvar1);
%put %superq(mvar2);
%put %superq(mvar3);

ods html file="c:\temp\sometext.html";

ods text="<p class=""bold"">%superq(mvar1)</p>";
ods text="<p class=""bold"">%superq(mvar2)</p>";
ods text="<p class=""bold"">%superq(mvar3)</p>";

proc print data=sashelp.class(obs=1);
run;

ods html close;

You can find a list of the HTML Entities here https://dev.w3.org/html5/html-author/charref

 

Bruno

Contributor
Posts: 59

Re: ODS TEXT with macro variable containing %-sign inside

[ Edited ]

Well thank you for the answer. That was indeed the problem in this case. However at least I get an warning on the log too that could not resolve macro %macroname if there is a % sign inside the ods text. So the next question is if my ods destination isn't html but something else how can I bypass that? Well that is more of a philosophical question since I got my issue solved.

Do you happen to know if there is a function in SAS which automatically translates special characters into HTML entities? The problem is that the text can contain basically any special character and I definately wouldn't want to hard code the transformation in every case.

 

Thanks again!

 

EDIT: Found the HTMLENCODE function, but unfortunately it does not encode all characters (especially % is missing).

SAS Super FREQ
Posts: 676

Re: ODS TEXT with macro variable containing %-sign inside

hi

 

There are the HTMLENCODE / HTMLDECODE functions, but they will not encode the % sign, they will others.

 

Bruno

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 225 views
  • 1 like
  • 2 in conversation