DATA Step, Macro, Functions and more

Reading a text file and maintaining format

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 83
Accepted Solution

Reading a text file and maintaining format

I have the following code which reads a text file (REF.txt) and changes the value at the &i.  The program works and changes the value but the output file (TEST) loses its format and is put out as continuous lines.  I have attached the file which I read in ref.ctl.  Can someone tell me how to edit my code so that it will read the ref.txt file and maintain the format in the outputted test1.txt etc files?

Thanks

 

 

data _null_ ;
infile '/folders/myfolders/Bootconcer/REF.CTL' truncover;
/* INPUT CHARACTER FILE*/
input c $10000.;


do i=1 to 10;
myfile='/folders/myfolders/Bootconcer/REFS/TEST' || trim(left(put(i,9.))) || '.ctl';

 

file dummy filevar=myfile MOD ;
if _n_=2 then do;
if i=1 then do;
amp_loc=index(c,'&');
first_half=substr(c,1,amp_loc-1);
last_half=substr(c,amp_loc+2);
end;

c= trim(first_half) || trim(left(put(i,9.))) || last_half;
end;

put c $;
end;
run;

 


Accepted Solutions
Solution
‎07-05-2017 10:06 AM
Super User
Posts: 6,938

Re: Reading a text file and maintaining format

Notepad is arguably the worst tool for dealing with text files that one can imagine. Discard it's use in favor of something usable like notepad++.

 

That said, to create DOS-compatible files, try to use the

termstr=CRLF

option in your file statement.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 6,938

Re: Reading a text file and maintaining format

[ Edited ]

The

/folders/myfolders

tells me you are using SAS University Edition, right?

This means that SAS runs on a UNIX (Linux), and the line separator will be a single '0a'x.

DOS (Windows) programs expect a sequence of '0d0a'x as line separators, so it depends on which software you use to view the result files.

Many editors (like notepad++) will display correctly and show UNIX in the status bar. Others will not be able to display the line changes, and you will see a continuous stream of characters.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 83

Re: Reading a text file and maintaining format

When I use notepad the output is continuous but if I change the it to a
*.doc file it reads okay.



The problem is that when I use the extension.ctl which is the name I need to
use the file it reverts back to the continuous output.

Yes I am using the University edition.



Is there another possible solution?

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Solution
‎07-05-2017 10:06 AM
Super User
Posts: 6,938

Re: Reading a text file and maintaining format

Notepad is arguably the worst tool for dealing with text files that one can imagine. Discard it's use in favor of something usable like notepad++.

 

That said, to create DOS-compatible files, try to use the

termstr=CRLF

option in your file statement.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 83

Re: Reading a text file and maintaining format

Should this go on the infile or the my file statement?

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 6,938

Re: Reading a text file and maintaining format


jacksonan123 wrote:
Should this go on the infile or the my file statement?

##- Please type your reply above this line. Simple formatting, no
attachments. -##

FILE statement, as it needs to influence your output. As it is, SAS produces correct output already, but certain non-UNIX software can't deal with it, do we need to make SAS write DOS-compatible output.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 83

Re: Reading a text file and maintaining format

I sent the file as an attachment and it looks okay but when I open it with
notepad there are no line breaks.

I put in the suggested code and the results were the same.



##- Please type your reply above this line. Simple formatting, no
attachments. -##
Frequent Contributor
Posts: 83

Re: Reading a text file and maintaining format

I put that code in the infile statement and got the same results-see attached file.

Super User
Super User
Posts: 6,500

Re: Reading a text file and maintaining format


jacksonan123 wrote:
When I use notepad the output is continuous but if I change the it to a
*.doc file it reads okay.



The problem is that when I use the extension.ctl which is the name I need to
use the file it reverts back to the continuous output.



Change the settings on your PC to always open *.cti files using WORDPAD instead of NOTEPAD.  That editor is not much different from notepad and it will properly edit files that use LF as the end of line characters.

 

Frequent Contributor
Posts: 83

Re: Reading a text file and maintaining format

Yes word pad works, but I can't find a *.ctl in my associate a file type or
protocol with a program choice list to connect to Word Pad.

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Super User
Posts: 6,500

Re: Reading a text file and maintaining format

Used to be in Windows you could right click on a file and selection Open With to tell it to open with a different program. There was a check box to say make this the default program.
Frequent Contributor
Posts: 83

Re: Reading a text file and maintaining format

I figured it out ( open a *.txt with Word pad which will be used always for
*.txt) then rename as *.ctl and everything works just fine.

My program can then read the file.

Thanks for the help.

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 5,083

Re: Reading a text file and maintaining format

Worth a try:

 

Change the logic that writes out the result this way:

 

put c;

 

Instead, try it this way:

 

put _infile_ @ amp_loc i z2.;

 

This would keep all characters in their original position, and place a two-digit version of i (with a leading zero if needed) where the ampersand appears.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 135 views
  • 0 likes
  • 4 in conversation