Hi experts,
I have been trying to update a txt file in place but I am not succeeding at it:
data _null_;
file "c:\test.txt";
if _n_=1 then do;
put 'Variable1|Variable2';
end;
put 'hi tst|blabla';
run;
data _null_;
infile "c:\test.txt" sharebuffers dsd dlm="|" lrecl=1014;
file "c:\test.txt";
variable1= tranwrd(variable1, "tst", "newtst");
run;
When I execute this code I get the following message:
NOTE: 0 records were read from the infile 'c:\test.txt'
NOTE: 0 records were read from the file'c:\test.txt'
The first row is a header.
Any inputs?
Thanks,
@bsas94 wrote:
Hi experts,
I have been trying to update a txt file in place but I am not succeeding at it:
data _null_; file "c:\test.txt"; if _n_=1 then do; put 'Variable1|Variable2'; end; put 'hi tst|blabla'; run; data _null_; infile "c:\test.txt" sharebuffers dsd dlm="|" lrecl=1014; file "c:\test.txt"; variable1= tranwrd(variable1, "tst", "newtst"); run;
When I execute this code I get the following message:
NOTE: 0 records were read from the infile 'c:\test.txt' NOTE: 0 records were read from the file'c:\test.txt'
The first row is a header.
Any inputs?
Thanks,
You don't show any INPUT statement that would read from the INFILE.
First. Before you do this make sure you have a recoverable copy of the original text file for when you make some mistake. Which considering your code not having any INPUT to read variable1 from the source is quite likely. Such as what is the LRECL on your FILE statement?
Second you would have to have a PUT statement to write the results.
How extensive are the changes to be made? If it is relatively minor you might be able to manipulate the automatic variable _infile_
Example:
data _null_; infile "c:\test.txt" sharebuffers dsd dlm="|" lrecl=1014; file "c:\outtest.txt" lrecl=1024; input ; _infile_= tranwrd(_infile_, "tst", "newtst"); put _infile_; run;
Note that I write out to a different file so you can compare results.
If you are making lots of changes it would like be better to read and create a data set, modify the set and then use proc export to create a new delimited text file.
Issues can arise about replacing short values with longer as the variables will only hold so many characters and you will need to read the input such that the longest expected replacement would actually fit in the allotted space (length of variable).
@bsas94 wrote:
Hi experts,
I have been trying to update a txt file in place but I am not succeeding at it:
data _null_; file "c:\test.txt"; if _n_=1 then do; put 'Variable1|Variable2'; end; put 'hi tst|blabla'; run; data _null_; infile "c:\test.txt" sharebuffers dsd dlm="|" lrecl=1014; file "c:\test.txt"; variable1= tranwrd(variable1, "tst", "newtst"); run;
When I execute this code I get the following message:
NOTE: 0 records were read from the infile 'c:\test.txt' NOTE: 0 records were read from the file'c:\test.txt'
The first row is a header.
Any inputs?
Thanks,
You don't show any INPUT statement that would read from the INFILE.
First. Before you do this make sure you have a recoverable copy of the original text file for when you make some mistake. Which considering your code not having any INPUT to read variable1 from the source is quite likely. Such as what is the LRECL on your FILE statement?
Second you would have to have a PUT statement to write the results.
How extensive are the changes to be made? If it is relatively minor you might be able to manipulate the automatic variable _infile_
Example:
data _null_; infile "c:\test.txt" sharebuffers dsd dlm="|" lrecl=1014; file "c:\outtest.txt" lrecl=1024; input ; _infile_= tranwrd(_infile_, "tst", "newtst"); put _infile_; run;
Note that I write out to a different file so you can compare results.
If you are making lots of changes it would like be better to read and create a data set, modify the set and then use proc export to create a new delimited text file.
Issues can arise about replacing short values with longer as the variables will only hold so many characters and you will need to read the input such that the longest expected replacement would actually fit in the allotted space (length of variable).
Without an input statement, nothing is read, and without a put, nothing is written. And you can't treat a text file (an unstructured stream of bytes) like a database table (where the file has a clear structure).
So you should write to a new file, and when you're done, delete the old and rename the new one.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.