BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
WesBarris
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20

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.

Data never sleeps

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20

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.

Data never sleeps
data_null__
Jade | Level 19

You don't need macro you need FILEVAR.

filename this "~/this.dat";
filename that "~/that.dat";

data _null_;
  
length filevar $128;
  
if (fexist("this")) then filevar = pathname('that','F');
   else filevar = pathname('this','F');
   putlog filevar=;

  
file dummy filevar=filevar dsd;
  
do while(not eof);
      set sashelp.class end=eof;
      put (_all_)(:);
     
end;
  
stop;
  
run;
WesBarris
Obsidian | Level 7

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.

data_null__
Jade | Level 19

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.

WesBarris
Obsidian | Level 7

Thank you.  I was able to do what I needed using your advice and creating a macro.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1544 views
  • 0 likes
  • 3 in conversation