ENCODING= Option

Overrides and transcodes the encoding for input or output processing of external files.
Valid in: %INCLUDE statement; FILE statement; FILENAME statement; FILENAME statement, EMAIL (SMTP) Access Method; INFILE statement; ODS statements; FILE command; INCLUDE command
Category: Data Access

Syntax

ENCODING= 'encoding-value'

Optional Argument

ENCODING= 'encoding-value'
specifies the encoding to use for reading, writing, copying, or saving an external file. The value for ENCODING= indicates that the external file has a different encoding from the current session encoding.
When you read, write, copy, or save data using an external file, SAS transcodes the data from the session encoding to the specified encoding.
Default:SAS uses the current session encoding.

Details

The following table provides information about how the ENCODING option is used with the corresponding statement:
%INCLUDE statement:
reads SAS statements and data lines from the specified source file (not supported under z/OS).
FILE statement:
writes to an external file.
FILENAME statement:
reads from or writes to an external file.
FILENAME statement, EMAIL (SMTP) Access Method:
sends electronic mail programmatically.
INFILE statement:
reads from an external file.
ODS statements:
controls features of the Output Delivery System that are used to generate, store, or reproduce SAS procedure and DATA step output.
FILE command:
saves the contents of a window to an external file.
INCLUDE command:
Copies an external file into the current window.
Some encodings use a Byte Order Mark (BOM). The BOM is generated when the encoding is specified. For the UTF-8 encoding, you must specify encoding=utf-8 on the filename and file DATA step statements in order for the BOM to be generated.

Examples

Example 1: Using the FILE Statement to Specify an Encoding for Writing to an External File

This example creates an external file from a SAS data set. The current session encoding is Wlatin1, but the external file's encoding needs to be UTF-8. By default, SAS writes the external file using the current session encoding.
To specify what encoding to use for writing data to the external file, specify the ENCODING= option:
libname myfiles 'SAS data-library';
filename outfile 'external-file';
data _null_;
   set myfiles.cars;
   file outfile encoding="utf-8";
   put Make Model Year;
run;
When you tell SAS that the external file is to be in UTF-8 encoding, SAS then transcodes the data from Wlatin1 to the specified UTF-8 encoding.

Example 2: Using the FILENAME Statement to Specify an Encoding for Reading an External File

This example creates a SAS data set from an external file. The external file is in UTF-8 character-set encoding, and the current SAS session is in the Wlatin1 encoding. By default, SAS assumes that an external file is in the same encoding as the session encoding, which causes the character data to be written to the new SAS data set incorrectly.
To specify which encoding to use when reading the external file, specify the ENCODING= option:
libname myfiles  'SAS data-library'; 
   
filename extfile 'external-file' encoding="utf-8"; 
data myfiles.unicode;                                                                                                                           
   infile extfile;                                                                                                     
   input Make $ Model $ Year;                                                                                                           
run;
When you specify that the external file is in UTF-8, SAS then transcodes the external file from UTF-8 to the current session encoding when writing to the new SAS data set. Therefore, the data is written to the new data set correctly in Wlatin1.

Example 3: Using the FILENAME Statement to Specify an Encoding for Writing to an External File

This example creates an external file from a SAS data set. By default, SAS writes the external file using the current session encoding. The current session encoding is Wlatin1, but the external file's encoding needs to be UTF-8.
To specify which encoding to use when writing data to the external file, specify the ENCODING= option:
libname myfiles  'SAS data-library'; 
filename outfile 'external-file' encoding="utf-8";                                                                                             
data _null_; 
   set myfiles.cars;                                                                                                                          
   file outfile;                                                                                                     
   put Make Model Year;                                                                                                           
run;
When you specify that the external file is to be in UTF-8 encoding, SAS then transcodes the data from Wlatin1 to the specified UTF-8 encoding when writing to the external file.

Example 4: Changing Encoding for Message Body and Attachment

This example illustrates how to change text encoding for the message body as well as for the attachment.
filename mymail email 'Joe.Developer@sas.com';
data _null_;
   file mymail
      subject='Text Encoding'
      encoding=greek 
      attach=('C:\My Files\Test.out' 
         content_type='text/plain' 
         encoding='ebcdic1047' 
         outencoding='latin1'); 
run;              
In the program, the following occurs:
  • The ENCODING= e-mail option specifies that the message body will be encoded to Greek (ISO) before being sent.
  • For the ATTACH= e-mail option, the attachment option ENCODING= specifies the encoding of the attachment that is read into SAS, which is Western (EBCDIC).
  • Because SMTP and other e-mail interfaces do not support EBCDIC, the attachment option OUTENCODING= converts the attachment to Western (ISO) before sending it.

Example 5: Using the INFILE= Statement to Specify an Encoding for Reading from an External File

This example creates a SAS data set from an external file. The external file's encoding is in UTF-8, and the current SAS session encoding is Wlatin1. By default, SAS assumes that the external file is in the same encoding as the session encoding, which causes the character data to be written to the new SAS data set incorrectly.
To specify which encoding to use when reading the external file, specify the ENCODING= option:
libname myfiles 'SAS data-library';
filename extfile 'external-file';
data myfiles.unicode;
   infile extfile encoding="utf-8";
   input Make $ Model $ Year;
run;
When you specify that the external file is in UTF-8, SAS then transcodes the external file from UTF-8 to the current session encoding when writing to the new SAS data set. Therefore, the data is written to the new data set correctly in Wlatin1.

See Also

Statements:
%INCLUDE Statement: UNIX in SAS Companion for UNIX Environments
%INCLUDE Statement: Windows in SAS Companion for Windows
FILE Statement in SAS Statements: Reference
FILENAME Statement in SAS Statements: Reference
INFILE Statement in SAS Statements: Reference
Commands:
FILE Command in SAS Companion for z/OS
FILE Command: UNIX in SAS Companion for UNIX Environments
FILE Command: Windows in SAS Companion for Windows
INCLUDE Command in SAS Companion for z/OS
INCLUDE Command: Windows in SAS Companion for Windows