[Please help] Export sas data with special characters into text files

Reply
New Contributor
Posts: 3

[Please help] Export sas data with special characters into text files

I would like to export a sas data to a text file. It is a fixed length file and I handle it in this way

 

data _null_;

set data end=last;

FILE "output.txt" ;

 

*** Details ***;

PUT

Field1 $1

Field2 $2-10

Field3 $11-20

.....

 

While this work only without any special characters in the data, once the variable contains any special character, it will automatically truncate the length of the row. (e.g. with 1 utf8 coding character in Field3, it will occupy 3bytes length and which result the final length trim to 8 instead of 10)

 

Please advice how can I handle it. Many thanks.

 

Trusted Advisor
Posts: 1,479

Re: [Please help] Export sas data with special characters into text files

[ Edited ]

Add ENCODING= option on FILE statement.

Available values you shall find in:

    https://support.sas.com/documentation/cdl/en/nlsref/61893/HTML/default/viewer.htm#a002607278.htm

 

Syntax like:

  FILE "output.txt"  encoding = "ANSI";

 

The encoding valur should fit the target platform of your output;

New Contributor
Posts: 3

Re: [Please help] Export sas data with special characters into text files

Thanks for your reply.

 

I have added

encoding="utf-8"

but it gives the same result which still truncate the variable length in output text files...

 

Trusted Advisor
Posts: 1,479

Re: [Please help] Export sas data with special characters into text files

If a character encoded 2 bytes you can't hold it in one byte.

What is the target  of the file ? is it WINDOWS / LINUX / MF ?

What language is the text ? 

If just latin alphanumeric and special character like dot, comma, etc. - 

then use encoding = ansi (one byte per character);

Trusted Advisor
Posts: 1,479

Re: [Please help] Export sas data with special characters into text files

UTF8 in some cases need two bytes
New Contributor
Posts: 3

Re: [Please help] Export sas data with special characters into text files

Yes, we have to use UTF8 as it contains Chinese Characters.

 

While I have to transfer the file to system so the length can't be changed.

 

Attached is a reference file which you can see even Chinese characters / numbers, each characters occpy the same length (using notepad+++ to check).

 

However, once the output generated from SAS, the length will be truncated.

Trusted Advisor
Posts: 1,479

Re: [Please help] Export sas data with special characters into text files

Are the special characters in a text significant ?

Can you omit them or replace them by space ? (using COMPRESS or TRANSLATE functions)

 

Otherwise - maybe the fix length should consider two bytes character as valid ?

Show examples to your system team and consult with them.

Super User
Posts: 9,875

Re: [Please help] Export sas data with special characters into text files

Try local format. Or $varying. 


data have;
input x : $40. y : $10.;
cards;
一二山石 打碎
1234 er
;
run;

data _null_;
set have;
 file '/folders/myfolders/want.txt';
 PUT
 @1 x  $kanji40. 
 @41 y  $kanji10. ;
run;
Ask a Question
Discussion stats
  • 7 replies
  • 311 views
  • 0 likes
  • 3 in conversation