How to check Encoding in SAS ?

The wrong encoding can produce errors or warnings like “transcode failed” or “data was lost during transcoding”, and so on! Whenever you see errors like these the first step would be to check the encoding set in the SAS.

There are three places where you have encoding specified and you must check it in SAS to start with the further investigation of your data encoding issue.

  1. Encoding of the SAS Session
  2. Encoding on SAS dataset
  3. Encoding on SAS library

You can have different encodings such as LATIN1, UTF-8, etc. Or rather I would say, you can modify and set different encoding within your application environment. 

Encoding set on source system or source file which is outside SAS should be investigated as well. The most of the time you have this encoding issue because encoding in SAS doesn’t match with the source system encoding. 

In this article we are going to see how you can check encoding in SAS.

1. Check Encoding of the SAS session

You can use proc options to know encoding of your current session. Here is the code to run in the code editor through either SAS studio, DI studio, or SAS EG.

/* check encoding of the sas session */

proc options option=encoding;
run;
SAS session encoding

Other option:

Alternatively, you can use the following code to print encoding of the SAS session.

%put %sysfunc(getOption(ENCODING));
encoding in SAS session

2. Check Encoding on SAS Dataset

To check default encoding on base SAS dataset, run the following code to check encoding on SAS dataset using proc content procedure.

Let’s have a quickly look at what encoding has been set on cars daatset from sashelp library.

proc content data=sashelp.cars; 
run;
check encoding on a SAS dataset

Other option:

Alternatively, you can use the following code to print encoding of a SAS dataset.

%let DSID=%sysfunc(open(sashelp.class, i));
%put %sysfunc(ATTRC(&DSID, ENCODING))
encoding on a SAS dataset

3. Check Encoding on SAS Library

SAS library doesn’t have a specific encoding on it. But data sets present under any library can have a different encoding.

For example, the “WORK” library has 10 data sets, the first 5 data sets have encoding utf-8 and the rest 5 data sets have encoding latin1.

Just for the demonstration purpose let me create two data sets with different encoding and store them in the same library.

The encoding on individual dataset can be easily identified by using proc content on entire list of data sets under any particular library.

/* create two data sets with different encoding in WORK library*/
data work.class(encoding='utf-8') work.class_2(encoding='latin1');
	set sashelp.class;
run;

/* list all the data sets from WORK library to see encoding details */
proc contents data=work._all_;
run;
SAS encoding on data sets in the library
SAS encoding on data sets in the library

FAQ

How to find out encoding set in SAS?

To find out the encoding in SAS, run the following code: proc options option=encoding; run; The encoding details will be printed in the sas log.

How do I know which encoding is used?

Run the below sas code in SAS studio, SAS GG, or any SAS code editor to know which is default encoding you have in the current session.

 

proc options option=encoding; run;
How to find out encoding set on SAS Dataset?

To find out the encoding set on SAS dataset, run the following proc content code and observe the output where encoding= option mentioned.

 

proc content data=sas_dataset_name; run;