DATA Step, Macro, Functions and more

How to use FEXIST in a data step?

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

How to use FEXIST in a data step?

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?


Accepted Solutions
Solution
‎04-10-2013 10:42 AM
Super User
Posts: 5,429

Re: How to use FEXIST in a data step?

Posted in reply to WesBarris

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


All Replies
Solution
‎04-10-2013 10:42 AM
Super User
Posts: 5,429

Re: How to use FEXIST in a data step?

Posted in reply to WesBarris

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
Respected Advisor
Posts: 3,799

Re: How to use FEXIST in a data step?

Posted in reply to WesBarris

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_)(Smiley Happy;
     
end;
  
stop;
  
run;
Contributor
Posts: 44

Re: How to use FEXIST in a data step?

Posted in reply to data_null__

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.

Respected Advisor
Posts: 3,799

Re: How to use FEXIST in a data step?

Posted in reply to WesBarris

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.

Contributor
Posts: 44

Re: How to use FEXIST in a data step?

Posted in reply to WesBarris

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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