Help using Base SAS procedures

Read in an ASCII file and updating it when character is found

Reply
Frequent Contributor
Posts: 82

Read in an ASCII file and updating it when character is found

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Read in an ASCII file and updating it when character is found

Posted in reply to SASHunter
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.
Contributor
Posts: 74

Re: Read in an ASCII file and updating it when character is found

Posted in reply to SASHunter
I guess you can do

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

and not have to do all the delete-output things.
Super Contributor
Super Contributor
Posts: 3,174

Re: Read in an ASCII file and updating it when character is found

Posted in reply to abdullala
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.
Respected Advisor
Posts: 3,799

Re: Read in an ASCII file and updating it when character is found

Posted in reply to SASHunter
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]
Frequent Contributor
Posts: 82

Re: Read in an ASCII file and updating it when character is found

Posted in reply to SASHunter
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.
Ask a Question
Discussion stats
  • 5 replies
  • 175 views
  • 0 likes
  • 4 in conversation