DATA Step, Macro, Functions and more

pc sas code move to unix error

Reply
Regular Contributor
Posts: 205

pc sas code move to unix error

Hello everyone,

 

I have a SAS code works well in PC environment. the SAS code does some text/flat file read and write.

But when I move it to unix. it casuse error. the reason is there are unexpected ^M at end of each rows (Enter Key). which cause error when I tried to do some string operation.

These ^M are invisible for windows notepad but visible for Unix vi editor.

 

Please help!

 

Thanks!

 

PROC Star
Posts: 288

Re: pc sas code move to unix error

Can you run the "dos2unix" facility on the files in Putty or some equivalent?

Regular Contributor
Posts: 205

Re: pc sas code move to unix error

tr -d '\15\32' < winfile.txt > unixfile.txt
Super User
Posts: 17,828

Re: pc sas code move to unix error

Have you tried specfying a TERMSTR type on your FILE statement?

Super User
Super User
Posts: 6,500

Re: pc sas code move to unix error

How did you move the text files between the two environments?  If you use FTP with ASCII mode then it should transform the CR+LF that DOS/WIndows uses as end of line into the LF that Unix uses as end of line.

If you are editing the files using NOTEPAD on a PC you might want to switch to using WORDPAD instead since it can handle text files with just LF as the end of line.

 

How are you reading the files in SAS?

You try adding the TERMSTR=CRLF to the appropriate INFILE, FILE or FILENAME statements to force all files in and out to use the DOS end of line convention.

 

But if you live in a mixed environment where PC and Unix users/apps are both reading and writing files to the same physical disk then you might need to make your input steps more flexible to handle files that have either format.

 

One idea is if you are reading the files are delimited files is to add the CR to the list of delimiters.

dlm=','||'0d'x;
infile 'myfile.txt' dsd dlm=dlm truncover firstobs=2;

Another is to add code to remove the CR from the line before running the INPUT statement.

input @;
_infile_ = compress(_infile_,'0d'x);
input ....;
Regular Contributor
Posts: 205

Re: pc sas code move to unix error

Thank you Tom,
Those method of 'INFILE' works great for reading, it can read flat /text file with Carriage Returns into SAS.
Now here is a new problem:
How if I want 'FILE' a flat/text file from UNIX? I want the output file with Carriage Returns.

Thanks!
Super User
Super User
Posts: 6,500

Re: pc sas code move to unix error

[ Edited ]

GeorgeSAS wrote:

Now here is a new problem:  If I want 'FILE' a flat/text file from UNIX? I want the output file with Carriage Returns.





 

You can add the TERMSTR=CRLF option to the FILE statement or FILENAME statement and SAS will write lines with both CR and LF at the end of the lines, which is the format that DOS/WIndows uses.

 

 

Regular Contributor
Posts: 205

Re: pc sas code move to unix error


Thanks!
Ask a Question
Discussion stats
  • 7 replies
  • 131 views
  • 4 likes
  • 4 in conversation