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;
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.
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.
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 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.
I put that code in the infile statement and got the same results-see attached file.
@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.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.