BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jacksonan123
Lapis Lazuli | Level 10

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

12 REPLIES 12
Kurt_Bremser
Super User

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.

jacksonan123
Lapis Lazuli | Level 10
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. -##
Kurt_Bremser
Super User

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.

jacksonan123
Lapis Lazuli | Level 10
Should this go on the infile or the my file statement?

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

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

jacksonan123
Lapis Lazuli | Level 10
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. -##
jacksonan123
Lapis Lazuli | Level 10

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

Tom
Super User Tom
Super User

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

 

jacksonan123
Lapis Lazuli | Level 10
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. -##
Tom
Super User Tom
Super User
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.
jacksonan123
Lapis Lazuli | Level 10
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. -##
Astounding
PROC Star

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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