<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Read in an ASCII file and updating it when character is found in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29335#M6871</link>
    <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
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.  &lt;BR /&gt;
&lt;BR /&gt;
Here is the code&lt;BR /&gt;
&lt;BR /&gt;
if index(intext,'0C'X) then do;&lt;BR /&gt;
                      put " ** ALERT!! - special character found.";&lt;BR /&gt;
                      pos = index(intext,'0C'X);&lt;BR /&gt;
                      put " Position of character is: " pos;&lt;BR /&gt;
&lt;BR /&gt;
                      /* Copy the problem file to save it first. */&lt;BR /&gt;
                      command='cp '||"&amp;amp;chkdir"||f2r||' '||fil2name;  &lt;BR /&gt;
                      call system(command);&lt;BR /&gt;
&lt;BR /&gt;
                      /* Remove this special character */&lt;BR /&gt;
                      txt2 = compress(intext,'0C'X);&lt;BR /&gt;
&lt;BR /&gt;
                      /* Output the corrected line back to the file */&lt;BR /&gt;
                      txt2 = compbl(txt2); &lt;BR /&gt;
                      FILE dummy;            /* write back to the file. */&lt;BR /&gt;
&lt;BR /&gt;
                      newln = txt2;&lt;BR /&gt;
                      newln = strip(newln);&lt;BR /&gt;
                      PUT @1 newln; &lt;BR /&gt;
                                            &lt;BR /&gt;
                      FILE log;&lt;BR /&gt;
             end;&lt;BR /&gt;
&lt;BR /&gt;
Thanks if anyone can give me help here.&lt;BR /&gt;
HunterN</description>
    <pubDate>Fri, 11 Dec 2009 16:53:52 GMT</pubDate>
    <dc:creator>SASHunter</dc:creator>
    <dc:date>2009-12-11T16:53:52Z</dc:date>
    <item>
      <title>Read in an ASCII file and updating it when character is found</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29335#M6871</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
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.  &lt;BR /&gt;
&lt;BR /&gt;
Here is the code&lt;BR /&gt;
&lt;BR /&gt;
if index(intext,'0C'X) then do;&lt;BR /&gt;
                      put " ** ALERT!! - special character found.";&lt;BR /&gt;
                      pos = index(intext,'0C'X);&lt;BR /&gt;
                      put " Position of character is: " pos;&lt;BR /&gt;
&lt;BR /&gt;
                      /* Copy the problem file to save it first. */&lt;BR /&gt;
                      command='cp '||"&amp;amp;chkdir"||f2r||' '||fil2name;  &lt;BR /&gt;
                      call system(command);&lt;BR /&gt;
&lt;BR /&gt;
                      /* Remove this special character */&lt;BR /&gt;
                      txt2 = compress(intext,'0C'X);&lt;BR /&gt;
&lt;BR /&gt;
                      /* Output the corrected line back to the file */&lt;BR /&gt;
                      txt2 = compbl(txt2); &lt;BR /&gt;
                      FILE dummy;            /* write back to the file. */&lt;BR /&gt;
&lt;BR /&gt;
                      newln = txt2;&lt;BR /&gt;
                      newln = strip(newln);&lt;BR /&gt;
                      PUT @1 newln; &lt;BR /&gt;
                                            &lt;BR /&gt;
                      FILE log;&lt;BR /&gt;
             end;&lt;BR /&gt;
&lt;BR /&gt;
Thanks if anyone can give me help here.&lt;BR /&gt;
HunterN</description>
      <pubDate>Fri, 11 Dec 2009 16:53:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29335#M6871</guid>
      <dc:creator>SASHunter</dc:creator>
      <dc:date>2009-12-11T16:53:52Z</dc:date>
    </item>
    <item>
      <title>Re: Read in an ASCII file and updating it when character is found</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29336#M6872</link>
      <description>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.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 11 Dec 2009 17:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29336#M6872</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-12-11T17:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: Read in an ASCII file and updating it when character is found</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29337#M6873</link>
      <description>I guess you can do &lt;BR /&gt;
&lt;BR /&gt;
intext= compress(intext,'0C'X);&lt;BR /&gt;
&lt;BR /&gt;
and not have to do all the delete-output things.</description>
      <pubDate>Fri, 11 Dec 2009 17:35:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29337#M6873</guid>
      <dc:creator>abdullala</dc:creator>
      <dc:date>2009-12-11T17:35:34Z</dc:date>
    </item>
    <item>
      <title>Re: Read in an ASCII file and updating it when character is found</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29338#M6874</link>
      <description>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;&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
Filename FT15F001 temp;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
   infile FT15F001 sharebuffers eof=eof;&lt;BR /&gt;
   file   FT15F001;&lt;BR /&gt;
   input;&lt;BR /&gt;
   if index(_infile_,'?') then do;&lt;BR /&gt;
      _infile_ = translate(_infile_,' ','?');&lt;BR /&gt;
      put _infile_;&lt;BR /&gt;
      end;&lt;BR /&gt;
   return;&lt;BR /&gt;
 eof: stop;&lt;BR /&gt;
parmcards;&lt;BR /&gt;
this line does not offend me&lt;BR /&gt;
this is a file where the ? is the offending character&lt;BR /&gt;
this is a file where the ? is the offending characterthis is a file where the ? is the offending character&lt;BR /&gt;
this line does not offend me&lt;BR /&gt;
?this is a file where the ? &lt;BR /&gt;
this line does not offend me&lt;BR /&gt;
this line does not offend me&lt;BR /&gt;
this line does not offend me&lt;BR /&gt;
this line does not offend me&lt;BR /&gt;
;;;;&lt;BR /&gt;
   run;&lt;BR /&gt;
data _null_; *after;&lt;BR /&gt;
   infile FT15F001;&lt;BR /&gt;
   input;&lt;BR /&gt;
   list;&lt;BR /&gt;
   run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 11 Dec 2009 17:41:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29338#M6874</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2009-12-11T17:41:15Z</dc:date>
    </item>
    <item>
      <title>Re: Read in an ASCII file and updating it when character is found</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29339#M6875</link>
      <description>Also, consider that you cannot just do a PUT &lt;YOURSASVARIABLE&gt;; 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.&lt;BR /&gt;
&lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;/YOURSASVARIABLE&gt;</description>
      <pubDate>Fri, 11 Dec 2009 17:45:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29339#M6875</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-12-11T17:45:37Z</dc:date>
    </item>
    <item>
      <title>Re: Read in an ASCII file and updating it when character is found</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29340#M6876</link>
      <description>Thank you everyone that replied.&lt;BR /&gt;
&lt;BR /&gt;
I used the _infile_ statement, the translate statement and the sharebuffers added to the INFILE statement.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
        /* The following command will actually open the external file. */&lt;BR /&gt;
        infile dummy filevar=fil2read filename=lname end=done &lt;BR /&gt;
                                     lrecl=200 truncover sharebuffers;&lt;BR /&gt;
.&lt;BR /&gt;
.&lt;BR /&gt;
&lt;BR /&gt;
             else if index(_infile_,'0C'X) then do;&lt;BR /&gt;
                      put " ** ALERT!! - special character found.";&lt;BR /&gt;
                      pos = index(intext,'0C'X);&lt;BR /&gt;
                      put " Position of character is: " pos;&lt;BR /&gt;
&lt;BR /&gt;
                      /* Copy the problem file to save it first. */&lt;BR /&gt;
                      command='cp '||"&amp;amp;chkdir"||f2r||' '||fil2name;  &lt;BR /&gt;
                      call system(command);&lt;BR /&gt;
&lt;BR /&gt;
                      /* Remove this special character */&lt;BR /&gt;
                      _infile_ = translate(_infile_,' ','0C'X);&lt;BR /&gt;
                      &lt;BR /&gt;
                      /* Output the corrected line back to the file */&lt;BR /&gt;
                      FILE dummy;            &lt;BR /&gt;
                      put _infile_;&lt;BR /&gt;
                                            &lt;BR /&gt;
                      FILE log;&lt;BR /&gt;
             end;&lt;BR /&gt;
&lt;BR /&gt;
.&lt;BR /&gt;
--------------------------------------------------------------------------------------------------------&lt;BR /&gt;
This worked perfectly!  &lt;BR /&gt;
Thanks so much.</description>
      <pubDate>Fri, 11 Dec 2009 18:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Read-in-an-ASCII-file-and-updating-it-when-character-is-found/m-p/29340#M6876</guid>
      <dc:creator>SASHunter</dc:creator>
      <dc:date>2009-12-11T18:51:56Z</dc:date>
    </item>
  </channel>
</rss>

