BookmarkSubscribeRSS Feed
GeorgeSAS
Lapis Lazuli | Level 10

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!

 

7 REPLIES 7
collinelliot
Barite | Level 11

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

GeorgeSAS
Lapis Lazuli | Level 10
tr -d '\15\32' < winfile.txt > unixfile.txt
Reeza
Super User

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

Tom
Super User Tom
Super User

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 ....;
GeorgeSAS
Lapis Lazuli | Level 10
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!
Tom
Super User Tom
Super User

@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.

 

 

GeorgeSAS
Lapis Lazuli | Level 10

Thanks!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 780 views
  • 4 likes
  • 4 in conversation