Previous Page | Next Page

Options for Commands, Statements, and Procedures for NLS

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
%INCLUDE statement: Reads SAS statements and data lines from the specified source file
Category: Data Access
%INCLUDE statement-specific: Is 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 from SAS using the SMTP (Simple Mail Transfer Protocol)
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

Syntax
Options
Examples
Example 1: Using the FILE Statement to Specify an Encoding for Writing to an External File
Example 2: Using the FILENAME Statement to Specify an Encoding for Reading an External File
Example 3: Using the FILENAME Statement to Specify an Encoding for Writing to an External File
Example 4: Changing Encoding for Message Body and Attachment
Example 5: Using the INFILE= Statement to Specify an Encoding for Reading from an External File
See Also

Syntax

ENCODING= 'encoding-value'


Options

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.

For details, see SBCS, DBCS, and Unicode Encoding Values for Transcoding Data.

Default: SAS uses the current session encoding.

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 1 
      attach=('C:\My Files\Test.out' 2 
         content_type='text/plain' 
         encoding='ebcdic1047' 
         outencoding='latin1'); 3 
run;              

In the program, the following occurs:

  1. The ENCODING= e-mail option specifies that the message body will be encoded to Greek (ISO) before being sent.

  2. 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).

  3. 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 in SAS Companion for OpenVMS on HP Integrity Servers

%INCLUDE in SAS Companion for UNIX Environments

%INCLUDE in SAS Companion for Windows

FILE in SAS Language Reference: Dictionary

FILENAME in SAS Language Reference: Dictionary

INFILE in SAS Language Reference: Dictionary

ODS statements that use encoding options in SAS Output Delivery System: User's Guide

Commands:

FILE in SAS Companion for OpenVMS on HP Integrity Servers

FILE in SAS Companion for z/OS

FILE in SAS Companion for UNIX Environments

FILE in SAS Companion for Windows

INCLUDE in SAS Companion for OpenVMS on HP Integrity Servers

INCLUDE in SAS Companion for z/OS

INCLUDE in SAS Companion for UNIX Environments

INCLUDE in SAS Companion for Windows

Previous Page | Next Page | Top of Page