Correctly escape angular bracets for html

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

Correctly escape angular bracets for html

Hi,

I have a macro variable that contains angular bracets. Since I need to output this macro variable in an html file, I need to convert these angular bracets in html < and > but, since & is reserved in sas it becomes a mess...

Here what I mean:

%let have = The test is <successful>;

I need to generate a macrovar want which must contain this value:

This text is in html and the macrovar have is in bold <b>The test is &lt;successful&gt;</b>

And then if I do %put &want; it must not resolve the &lt and &gt, but it must put exactly that value.

 

I hope that's understandable, I'm getting confused...

Thanks


Accepted Solutions
Solution
‎03-22-2016 07:16 AM
Super User
Super User
Posts: 7,720

Re: Correctly escape angular bracets for html

Yes, and as I said in the first post, why do you need to use macro language, again it is Not for manipulating data, use Base SAS:

%let have = The test is <successful>;

data _null_;
  tmp=cats('This text is in html and the macrovar have is in bold <b>',htmlencode("&have."),'</b>');
  put tmp;
run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,720

Re: Correctly escape angular bracets for html

[ Edited ]

Use HMTLENCODE:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001157104.htm

 

Should do that for you.  

 

Oh, also to add, why do you need it as a macro variable.  Putting the information in a dataset will make your life a lot easier.  Macro is for generating code, Not for processing and manipulating data!

Contributor
Posts: 43

Re: Correctly escape angular bracets for html

I tried to do this:

 

%let have = The test is <successful>;
%let want = This text is in html and the macrovar have is in bold <b>%sysfunc(htmlencode(&have))</b>;
%put &want;

But I've got errors:

23         %let have = The test is <successful>;
24         %let want = This text is in html and the macrovar have is in bold <b>%sysfunc(htmlencode(&have))</b>;
WARNING: Apparent symbolic reference LT not resolved.
WARNING: Apparent symbolic reference GT not resolved.
NOTE: Line generated by the macro function "SYSFUNC".
24         >The test is &lt;successful&gt;
                            __________
                            180
ERROR 180-322: Statement is not valid or it is used out of proper order.

24         %let want = This text is in html and the macrovar have is in bold <b>%sysfunc(htmlencode(&have))</b>;
                                                                                                           _
                                                                                                           180
ERROR 180-322: Statement is not valid or it is used out of proper order.

WARNING: Apparent symbolic reference LT not resolved.
25         %put &want;
This text is in html and the macrovar have is in bold <b>The test is &lt
Solution
‎03-22-2016 07:16 AM
Super User
Super User
Posts: 7,720

Re: Correctly escape angular bracets for html

Yes, and as I said in the first post, why do you need to use macro language, again it is Not for manipulating data, use Base SAS:

%let have = The test is <successful>;

data _null_;
  tmp=cats('This text is in html and the macrovar have is in bold <b>',htmlencode("&have."),'</b>');
  put tmp;
run;
Contributor
Posts: 43

Re: Correctly escape angular bracets for html

[ Edited ]

You're right!

I have a final issue: this macro var is the argument of a macro function which send an email. How can I correctly call the macro?

E.g.:

%macro sendmail(body);
%put I will send the mail with body=&body;
%mend;

%sendmail(&want); /* ERROR 180-322: Statement is not valid or it is used out of proper order, because there is the symbol ';' I guess

 

EDIT: sorry I am mistaken, the macro call works as well.

Thanks a lot!

Super User
Super User
Posts: 7,720

Re: Correctly escape angular bracets for html

%let have = The test is <successful>;

data email_dataset;
  to="abc@gmail.com";
  body=cats('This text is in html and the macrovar have is in bold 
run;

%Create_Email_Macro (ds=email_dataset);

So create a dataset with your email, then pass the dataset into your macro.   Its a lot easier to keep data in datasets, and use those.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 408 views
  • 0 likes
  • 2 in conversation