HTMLENCODE Function

Encodes characters using HTML character entity references, and returns the encoded string.

Category: Web Tools
Restriction: I18N Level 2 functions are designed for use with SBCS, DBCS, and MBCS (UTF8).

Syntax

HTMLENCODE(expression, <options>)

Required Argument

expression

specifies a character constant, variable, or expression. By default, any greater-than (>), less-than (<), and ampersand (&) characters are encoded as &gt;, &lt;, and &amp;, respectively. In SAS 9 only, this behavior can be modified with the options argument.

Note: The encoded string can be longer than the output string. You should take the additional length into consideration when you define your output variable. If the encoded string exceeds the maximum length that is defined, the output string might be truncated.

Optional Argument

options

is a character constant, variable, or expression that specifies the type of characters to encode. If you use more than one option, separate the options by spaces. The following options are available:

Option
Character
Character Entity Reference
Description
amp
&
&amp;
The HTMLENCODE function encodes these characters by default. If you need to encode these characters only, then you do not need to specify the options argument. However, if you specify any value for the options argument, then the defaults are overridden, and you must explicitly specify the options for all of the characters that you want to encode.
gt
>
&gt;
lt
<
&lt;
apos
'
&apos;
Use this option to encode the apostrophe ( ' ) character in text that is used in an HTML or XML tag attribute.
quot
"
&quot;
Use this option to encode the double quotation mark (") character in text that is used in an HTML or XML tag attribute.
7bit
any character that is not represented in 7-bit ASCII encoding
&#xnnn; (Unicode)
nnn is a one or more digit hexadecimal number. Encode these characters to create HTML or XML that is easily transferred through communication paths that might support only 7-bit ASCII encodings (for example, ftp or e-mail).

Example

The following SAS statements produce these results.
SAS Statement
Result
htmlencode("John's test <tag>")
John's test &lt;tag&gt;
htmlencode("John's test <tag>",'apos')
John&apos;s test <tag>
htmlencode('John "Jon" Smith <tag>','quot')
John &quot;Jon&quot; Smith <tag>
htmlencode("'A&B&C'",'amp lt gt apos')
&apos;A&amp;B&amp;C&apos;
htmlencode('80'x, '7bit')
('80'x is the euro symbol in Western European locales.)
&#x20AC; 
(20AC is the Unicode code point for the euro symbol.)

See Also

Functions: