I would like to prevent an output file from SAS from being overwritten if that SAS program is run a second time. To do this I have assigned two filerefs using two FILENAME statements. Then in the data step that writes the output, I have tried to use the FEXIST function to test if the output file already exists. If it does, I would like to write the output to a second file. Here is some pseudo code I have written:
filename this "C:\this.dat";
filename that "C:\that.dat";
.
.
.
data here;
set there;
if (fexist("this")) then
file that;
else
file this;
put a b c d;
run;
When I use this code, the file "this.dat" is always written and the file "that.dat" is always zeroed out. What am I doing wrong?
You might be better off by checking this before the data step in a macro, and assign the appropriate file name to macro variable, and then avoiding having the explicit FILE statements for each file.
You might be better off by checking this before the data step in a macro, and assign the appropriate file name to macro variable, and then avoiding having the explicit FILE statements for each file.
You don't need macro you need FILEVAR.
Data_null,
I was unable to get your solution to work. The first iteration of the data step would write to one file. Subsequent iterations would write to the other file. I was unable to understand what you were doing with that do while loop. I wasn't sure if that was something that I needed or not.
The DO WHILE loop is the part that keeps it from doing what you don't want it to do but once (check for existence and open appropriate file). You are suppose to put your SET statement in the same place as mine.
Thank you. I was able to do what I needed using your advice and creating a macro.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.