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

I need to delete a file in libname if it exists.

It's not right...

 

%IF %SYSFUNC(TODAY(),DAY.) EQ 1 %THEN %DO;
%IF %SYSFUNC(FILEEXIST(DRM.TRANSF_PARTIAL_&MES.)) %THEN %DO;

%SYSFUNC(FDELETE(DRM.TRANSF_PARTIAL_&MES.));
%END;
%LET FILE=TRANSF_CLOSURE_&MES.;
%END;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you want to delete a dataset then why not just use PROC DELETE?

%if %sysfunc(today(),day.) eq 1 %then %do;
  %let file=TRANSF_CLOSURE_&MES.;
  %if %sysfunc(exist(drm.&file)) %then %do;
proc delete data=drm.&file ;
run;
  %end;
%end;

View solution in original post

6 REPLIES 6
ballardw
Super User

Are you attempting to delete a SAS date set or an external non-SAS file? FILEEXIST function checks for the existence of external files, not SAS data sets. You would use EXIST function to determine if a SAS data set or view is present.

 

FDELETE is intended to delete external files and the reference would be "is a character constant, variable, or expression that specifies the fileref that you assign to the external file or directory. " not a libname.dataset construct. You would have to generate an operating system path/filename instead of the library.dataset reference for Fdelete to work.

 

 

a_ramos
Obsidian | Level 7

Yes, it is external.

Please, how can I put FILEREF in the macro?

Is there a way to delete this file just by pointing to libname?

Tom
Super User Tom
Super User

If you want to delete a dataset then why not just use PROC DELETE?

%if %sysfunc(today(),day.) eq 1 %then %do;
  %let file=TRANSF_CLOSURE_&MES.;
  %if %sysfunc(exist(drm.&file)) %then %do;
proc delete data=drm.&file ;
run;
  %end;
%end;
a_ramos
Obsidian | Level 7

It worked perfectly when I simulated day 1, thank you.
I was confusing the EXIST functions with FILEEXIST and FDELETE with PROC DELETE. Thanks

 

%IF %SYSFUNC(TODAY(),DAY.) EQ 1 %THEN %DO;
%IF %SYSFUNC(EXIST(DRM.TRANSF_PARTIAL_&MES.)) %THEN %DO;
PROC DELETE DATA=DRM.TRANSF_PARTIAL_&MES.;
%END;
%LET FILE=TRANSF_CLOSURE_&MES.;
%END;

 

%ELSE %DO;
%LET FILE=TRANSF_PARTIAL_&MES.;
%END;

 

Reeza
Super User

@a_ramos wrote:

I need to delete a file in libname if it exists.

It's not right...

 

%IF %SYSFUNC(TODAY(),DAY.) EQ 1 %THEN %DO;
%IF %SYSFUNC(FILEEXIST(DRM.TRANSF_PARTIAL_&MES.)) %THEN %DO;

%SYSFUNC(FDELETE(DRM.TRANSF_PARTIAL_&MES.));
%END;
%LET FILE=TRANSF_CLOSURE_&MES.;
%END;


How is it not right? Are you getting an error? Is it not working? I'm assuming this is wrapped in a MACRO?

 

Your condition will never be true until the first day of the month so you'll need to change that to test it for sure. 

a_ramos
Obsidian | Level 7

I was confusing the EXIST functions with FILEEXIST and FDELETE with PROC DELETE.
Tom helped me.
Thanks

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1505 views
  • 2 likes
  • 4 in conversation