Sirus Dehdari
Sirus Dehdari

Reputation: 3

Overwriting text in file using FILE Statement

I have a data set, call it Data1 with just one variable, V1, and from this I would like to save each entry in a text-file, do some stuff with the text-file, and then move on to the next entry. When the next entry is written to the text-file, I want it to overwrite the previous entry. I am using the FILE statement, but for some reason, it keeps appending the new entry to the old one. I have the following code:

data _null_;
  set Data1;
  do i = _N_;
    if i > 0 then do;
      file "myfile.txt";
      put V1;
    end;
  end;
run;

When I open myfile.txt, I see that the entries for all observations have been saved, one row for each. However, what I want is that only the last entry should be saved (I will add other stuff to the loop later, but what's important is that the old content is overwritten).

I tried adding the option "OLD" to the FILE statement, but I get a message saying that OLD is no longer supported. I am using SAS 9.4.

Thank you in advance!

Upvotes: 0

Views: 902

Answers (2)

mjsqu
mjsqu

Reputation: 5452

As specified in the comments, this is a quick and dirty way of doing it with double trailing-at, and pointer position:

data _null_;
set Data1;
filler=repeat(' ',200);
file "myfile.txt";
  put @1 filler @@;
  put @1 V1 @@;
run;

Additions:

  • The filler variable keeps 200 spaces (increase if more whitespace needed)
  • The first put statement writes the whitespace to the first line in the file, the double trailing-at holds the pointer on the current line
  • The second put statement writes the V1 value at the first column, overwriting some of the whitespace
  • When the data step loops again, the first put writes whitespace to the first line...

etc.

Removed:

  • Do loops, I'm not sure if they're required or were just an attempt to recreate the file each time a new line was reached. A data step by default loops _N_ from 1 to the end of the input.

Upvotes: 0

Joe
Joe

Reputation: 63424

It's hard to tell what you're trying to do exactly, but one possible answer is to use the _FILE_ automatic variable.

data _null_;
  set sashelp.class;
  do i = 1 to _N_;
    if i > 0 then do;
      file "c:\temp\myfile.txt";
      _file_ = name;
    end;
  end;
run;

This is an advanced SAS technique known as _FILE_ Magic. It's related to _INFILE_ Magic, a more well known technique, and is quite helpful if you'd like to do something such as use the powerful scanning tools available to input but not as readily available in functions (though that's less true now than it was 10 years ago; most of what you can do in input is now available via scan, index, find, etc.). If that's why you want to do this, then you may want to use either _FILE_ or _INFILE_ depending on what you are doing exactly. _INFILE_ version which uses sharebuffers to allow you to read/write the same file:

data _null_;
  set sashelp.class;
  do i = 1 to _N_;
    if i > 0 then do;
      infile "c:\temp\myfile.txt" sharebuffers;
      file "c:\temp\myfile.txt";
      _infile_ = name;
    end;
  end;
run;

Search for papers on the topic to see more details; Mike Zdeb's _FILE_ Magic (NESUG 2012), and Peter Crawford's More _INFILE_ Magic (SUGI 28/2003) are excellent examples of these tools.

Upvotes: 1

Related Questions