'SAS: Change encoding in proc import
I've got proc import from xlsx file with column names in polish language.
My simple proc looks like this:
proc import datafile = '/directory/file_name.XLSX'
out = libname.tablename
dbms = xlsx
replace;
run;
I would like to add somewhere ENCODING="LATIN2" so the columns don't look like:
Is it possible? And how?
I could do it in second step by renaming all the columns with some predefined list. but I don't want to do it like this yet. Maybe there is a better solution.
Solution 1:[1]
You need to specify the encoding of the file you are reading/importing.
Per SAS support, this can be specified in the filename statement.
I've tested it with SAS UE and csv files and it worked pretty well:
filename temp '/folders/myfolders/Raw data/iso8859.csv' encoding="utf-8";
proc import datafile = temp
out = utf8
dbms = csv
replace;
run;
Your code should then look like:
filename temp '/directory/file_name.XLSX' encoding="LATIN2";
proc import datafile = temp
out = libname.tablename
dbms = xlsx
replace;
run;
Solution 2:[2]
There's a few things going on here:
- You can't control the encoding of the XLSX format file; that's a binary file (sort of), and SAS doesn't treat it like a text file. You can do this for CSVs, as that's read in as a text file, but not XLSX.
- If you're importing a file in another encoding into SAS, your session encoding also matters. You will have to run your SAS session in the right encoding to have everything look right. See the documentation for more details on how to change your SAS session encoding.
But, there's a third option here, if you just want to get rid of the extra characters: options validvarname=v7 (or even v6). I believe this will prevent SAS's import engine from using any character other than A-Z 0-9 and underscore. It won't necessarily look pretty, though, it'll replace all of the other characters with underscores I suspect.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | FloT |
| Solution 2 | Joe |

