Character data loss occurs during transcoding


When a SAS data set encoded with a different character encoding than the current SAS session is read, automatic transcoding might fail and generate errors similar to the following:

ERROR: Some character data in the data set "XXX.XXX" was lost during transcoding. Truncation occurred during transcoding to the new encoding. To avoid the transcoding error, please refer to "Troubleshooting Truncation and Data loss Issue during Transcoding" in SAS National Language Support (NLS): reference guide.

The error might also occur after you do the following:

In addition, any of the methods described in SAS KB0039558 might produce the error

This issue can occur in any environment where automatic transcoding is performed between different encodings, including:

Here is an example:

Note: The behavior is not limited to MS-950 and might occur with any encoding when invalid byte sequences exist in the source data.

Cause

The failure is not necessarily caused by insufficient target variable length.

The Raw Bytes Analysis might reveal that the affected value contains byte sequence that were not valid for the dataset's declared encoding 

When SAS performs automatic transcoding, the following occurs:

  1. SAS reads the dataset encoding metadata.
  2. SAS interprets the source bytes according to that encoding.
  3. SAS converts the characters into the session encoding.

If invalid or corrupted byte sequences exist in the source data, SAS might be unable to correctly interpret and convert the value, which results in transcoding errors or character data loss.

Because the issue originates from invalid source bytes rather than character length expansion, methods intended to address length growth during transcoding do not resolve the problem.

Resolution 

To resolve this issue, assign the source library using ASCIIANY, and convert the data explicitly.

In the following example, a SAS data set encoded in MS-950 was created in a SAS9 environment on Windows and needs to be migrated to a SAS Viya environment on Linux running with UTF-8 encoding.
  1. Assign the source library using the encoding as 'ASCIIANY'. In this example, the dataset located in '/path/to/data' is assigned to the libref 'mylib'. Specifying encoding as 'ASCIIANY' instructs SAS to bypass automatic transcoding and read the character data as raw bytes, allowing the data to be examined and converted explicitly.

    libname mylib '/path/to/data' encoding=asciiany;
     
  2. Convert character data explicitly by using KCVT function from 'MS-950' to 'UTF-8'. 

    data sample_converted;
        set mylib.mydata;
        sample_column_utf8= kcvt(sample_column,'ms-950','utf-8');
    run;

Additional Information

For more information about the use of SAS functions and statements, see the following resources: