Reading UTF-8 in Filename Pipe
Having been blessed with an en-dash (alt + 2013) in our TFS project root, I’ve been having lots of fun getting SAS to ‘play nicely’ with non-ascii characters when running OS commands (eg tf.exe).
Storing the values in macro variables works fine for direct use (eg in an X command), but running a SAS generated .bat file was proving challenging - until I realised that the issue was actually within the windows shell, rather than SAS itself.
The solution turned out to be twofold. Firstly, the batch file itself needed to be UTF-8 encoded WITHOUT the BOM marker. This required the nobomfile system option to be in place before creating the file.
Secondly, the shell needs to be configured to read UTF-8 characters. This can be done by changing the codepage (using chcp 65001) and chaining with an ampersand.
Sample code as follows:
options nobomfile;data null;
%let work=%sysfunc(pathname(work));
%let file=&work/mybatchfile.bat;
file ”&file” encoding=‘utf-8’;
put “dir ""&work"" ”;
put ‘echo Nonstandard – Dash’;
run;
filename mypipe pipe “chcp 65001 & ""&file"" ”;
data null;infile mypipe;input;list;run;