BookmarkSubscribeRSS Feed
SASHunter
Obsidian | Level 7
Hi,

I have a program that I am writing where I read in an ASCII file and search each line to see if it has any special control characters in it. What I would like to do is, when I find a line that has one of these special control characters (ex. FF) in it, I want to update the line right then and write back to it. The following code read the file and writes back to it. It writes back to it fine but, it also put an extra line after the line I just wrote. I can't tell if it is an extra line or just the line that it wrote plus with extra spaces wrapping with it.

Here is the code

if index(intext,'0C'X) then do;
put " ** ALERT!! - special character found.";
pos = index(intext,'0C'X);
put " Position of character is: " pos;

/* Copy the problem file to save it first. */
command='cp '||"&chkdir"||f2r||' '||fil2name;
call system(command);

/* Remove this special character */
txt2 = compress(intext,'0C'X);

/* Output the corrected line back to the file */
txt2 = compbl(txt2);
FILE dummy; /* write back to the file. */

newln = txt2;
newln = strip(newln);
PUT @1 newln;

FILE log;
end;

Thanks if anyone can give me help here.
HunterN
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Just use the TRANSLATE function against _INFILE_ variable to convert all occurences of one character value to another, all done in one instruction. Then do your PUT back to an output file - one that has the same LRECL as your input.

Scott Barry
SBBWorks, Inc.
abdullala
Calcite | Level 5
I guess you can do

intext= compress(intext,'0C'X);

and not have to do all the delete-output things.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Also, consider that you cannot just do a PUT ; out to the output file, because your variable length is likely not the exact length as what was the input record length. So, you need to add a useful parameter to your INFILE statement that tells you the record_length and then when writing out to the "new" file, supply this variable value while using $VARYING. output file.

Also, I may have implied that your mention to the phrase "...I want to update the line right then.." was that you intended to substitute another character. Maybe not - so TRANSLATE may not be useful for this application.

Based on your stated symptom, you should investigate my reply comments about the INFILE and PUT statement considerations when writing a variable-sized output record.

Scott Barry
SBBWorks, Inc.
data_null__
Jade | Level 19
You can update the file in place with the SHAREBUFFERS INFILE statement option. You read each line INPUT; look at _INFILE_ for offending characters if they exist translate and put;

[pre]
Filename FT15F001 temp;
data _null_;
infile FT15F001 sharebuffers eof=eof;
file FT15F001;
input;
if index(_infile_,'?') then do;
_infile_ = translate(_infile_,' ','?');
put _infile_;
end;
return;
eof: stop;
parmcards;
this line does not offend me
this is a file where the ? is the offending character
this is a file where the ? is the offending characterthis is a file where the ? is the offending character
this line does not offend me
?this is a file where the ?
this line does not offend me
this line does not offend me
this line does not offend me
this line does not offend me
;;;;
run;
data _null_; *after;
infile FT15F001;
input;
list;
run;
[/pre]
SASHunter
Obsidian | Level 7
Thank you everyone that replied.

I used the _infile_ statement, the translate statement and the sharebuffers added to the INFILE statement.


/* The following command will actually open the external file. */
infile dummy filevar=fil2read filename=lname end=done
lrecl=200 truncover sharebuffers;
.
.

else if index(_infile_,'0C'X) then do;
put " ** ALERT!! - special character found.";
pos = index(intext,'0C'X);
put " Position of character is: " pos;

/* Copy the problem file to save it first. */
command='cp '||"&chkdir"||f2r||' '||fil2name;
call system(command);

/* Remove this special character */
_infile_ = translate(_infile_,' ','0C'X);

/* Output the corrected line back to the file */
FILE dummy;
put _infile_;

FILE log;
end;

.
--------------------------------------------------------------------------------------------------------
This worked perfectly!
Thanks so much.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 846 views
  • 0 likes
  • 4 in conversation