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

Hello

I am trying to delete a folder with all the files that are located there. (SAS data sets/XLSX files/ CSV flies).

 

data _null_;
    fname="TYOTA";
    rc=filename(fname, "/usr/local/SAS/SASUsers/LabRet/UserDir/RRR/TYOTA");
    if rc = 0 and fexist(fname) then
       rc=fdelete(fname);
    rc=filename(fname);
run;

When I check then I see that nothing happened and the file was not deleted.

 

Please see the log

 

1                                                          The SAS System                         07:42 Wednesday, November 24, 2021

1          ;*';*";*/;quit;run;
2          OPTIONS PAGENO=MIN;
3          %LET _CLIENTTASKLABEL='Program';
4          %LET _CLIENTPROCESSFLOWNAME='Process Flow';
5          %LET _CLIENTPROJECTPATH='';
6          %LET _CLIENTPROJECTPATHHOST='';
7          %LET _CLIENTPROJECTNAME='';
8          %LET _SASPROGRAMFILE='';
9          %LET _SASPROGRAMFILEHOST='';
10         
11         ODS _ALL_ CLOSE;
12         OPTIONS DEV=PNG;
13         GOPTIONS XPIXELS=0 YPIXELS=0;
14         FILENAME EGSR TEMP;
15         ODS tagsets.sasreport13(ID=EGSR) FILE=EGSR
16             STYLE=HTMLBlue
17             STYLESHEET=(URL="file:///C:/Program%20Files/SASHome/SASEnterpriseGuide/7.1/Styles/HTMLBlue.css")
18             NOGTITLE
19             NOGFOOTNOTE
20             GPATH=&sasworklocation
21             ENCODING=UTF8
22             options(rolap="on")
23         ;
NOTE: Writing TAGSETS.SASREPORT13(EGSR) Body file: EGSR
24         
25         GOPTIONS ACCESSIBLE;
26         data _null_;
27             fname="TYOTA";
28             rc=filename(fname, "/usr/local/SAS/SASUsers/LabRet/UserDir/RRR/TYOTA");
29             if rc = 0 and fexist(fname) then
30                rc=fdelete(fname);
31             rc=filename(fname);
32         run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      user cpu time       0.00 seconds
      system cpu time     0.00 seconds
      memory              350.53k
      OS Memory           19616.00k
      Timestamp           11/24/2021 08:25:19 AM
      Step Count                        24  Switch Count  0
      Page Faults                       0
      Page Reclaims                     53
      Page Swaps                        0
      Voluntary Context Switches        2
      Involuntary Context Switches      0
      Block Input Operations            0
      Block Output Operations           0
      

33         
34         
35         GOPTIONS NOACCESSIBLE;
36         %LET _CLIENTTASKLABEL=;
37         %LET _CLIENTPROCESSFLOWNAME=;
38         %LET _CLIENTPROJECTPATH=;
39         %LET _CLIENTPROJECTPATHHOST=;
2                                                          The SAS System                         07:42 Wednesday, November 24, 2021

40         %LET _CLIENTPROJECTNAME=;
41         %LET _SASPROGRAMFILE=;
42         %LET _SASPROGRAMFILEHOST=;
43         
44         ;*';*";*/;quit;run;
45         ODS _ALL_ CLOSE;
46         
47         
48         QUIT; RUN;
49         
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
/*Firstly delete all the files under a fold*/
data have;
rc=filename('xx','/data/sas_data/work/');
did=dopen('xx');
do i=1 to dnum(did);
 fname=dread(did,i);
 output;
end;
rc=dclose(did);
run;
%macro dfile(fname=);
data _null_;
rc=filename('temp',"/data/sas_data/work/&fname");
if rc=0 and fexist('temp') then rc=fdelete('temp');
rc=filename('temp');
put _all_;
run;
%mend;
data _null_;
 set have;
 call execute(cats('%dfile(fname=',fname,')'));
run;








/*Secondly, delete this empty fold*/
%macro dir(fname=);
filename x "/data/sas_data/work/&fname";
data _null_;
 rc=fdelete('x');
 put rc=;
 msg=sysmsg();
 put msg=;
run;
%mend;
data _null_;
 set have;
 call execute(cats('%dir(fname=',fname,')'));
run;



View solution in original post

10 REPLIES 10
Ronein
Meteorite | Level 14

Hello

I want to delete specific files  that are located in specific path .

Some of the files are SAS data set and other are CSV files or XLSX files.

When I run this code to delete data set Revenue 2020 then it is working 100% and the data set is deleted.

When I run this code to delete  CSV file called XXX8   then it is not working .

I see in Log a message 

NOTE: The file RRR.XXX8 (memtype=DATA) was not found, but appears on a DELETE statement.

May anyone help please?

 

libname RRR "/usr/local/SAS/SASUsers/LabRet/UserDir/RRR/";
proc datasets library=RRR;
delete  Revenue2020;
run;

/***This code is not working***/

libname RRR "/usr/local/SAS/SASUsers/LabRet/UserDir/RRR/";
proc datasets library=RRR;
delete xxx8;
run;
ChrisNZ
Tourmaline | Level 20

Use the fdelete() function to delete non-SAS files.

Or run a system command if you have XCMD enabled and have many files to delete.

Ronein
Meteorite | Level 14

Thank you.

Is it necessary to add the file extension to file name?

I run the following code and the file was not deleted

data _null_;
rc = filename('peletNewNew', "/usr/local/SAS/SASUsers/LabRet/UserDir/RRR/peletNewNew");
put 'peletNewNew' rc=;
if rc = 0 and fexist('peletNewNew') then do;
rc=fdelete('peletNewNew');
put 'peletNewNew ' rc=;
end;
rc=filename('peletNewNew');
put rc=;
run;

The Log show the following

1                                                          The SAS System                         07:42 Wednesday, November 24, 2021

1          ;*';*";*/;quit;run;
2          OPTIONS PAGENO=MIN;
3          %LET _CLIENTTASKLABEL='Program';
4          %LET _CLIENTPROCESSFLOWNAME='Process Flow';
5          %LET _CLIENTPROJECTPATH='';
6          %LET _CLIENTPROJECTPATHHOST='';
7          %LET _CLIENTPROJECTNAME='';
8          %LET _SASPROGRAMFILE='';
9          %LET _SASPROGRAMFILEHOST='';
10         
11         ODS _ALL_ CLOSE;
12         OPTIONS DEV=PNG;
13         GOPTIONS XPIXELS=0 YPIXELS=0;
14         FILENAME EGSR TEMP;
15         ODS tagsets.sasreport13(ID=EGSR) FILE=EGSR
16             STYLE=HTMLBlue
17             STYLESHEET=(URL="file:///C:/Program%20Files/SASHome/SASEnterpriseGuide/7.1/Styles/HTMLBlue.css")
18             NOGTITLE
19             NOGFOOTNOTE
20             GPATH=&sasworklocation
21             ENCODING=UTF8
22             options(rolap="on")
23         ;
NOTE: Writing TAGSETS.SASREPORT13(EGSR) Body file: EGSR
24         
25         GOPTIONS ACCESSIBLE;
26         data _null_;
27         rc = filename('peletNewNew', "/usr/local/SAS/SASUsers/LabRet/UserDir/RRR/peletNewNew");
28         put 'peletNewNew' rc=;
29         if rc = 0 and fexist('peletNewNew') then do;
30         rc=fdelete('peletNewNew');
31         put 'peletNewNew ' rc=;
32         end;
33         rc=filename('peletNewNew');
34         put rc=;
35         run;

peletNewNewrc=20014
rc=20014
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      user cpu time       0.01 seconds
      system cpu time     0.00 seconds
      memory              350.53k
      OS Memory           19616.00k
      Timestamp           11/24/2021 09:36:44 AM
      Step Count                        26  Switch Count  0
      Page Faults                       0
      Page Reclaims                     53
      Page Swaps                        0
      Voluntary Context Switches        4
      Involuntary Context Switches      0
      Block Input Operations            0
      Block Output Operations           0
      

36         
37         GOPTIONS NOACCESSIBLE;
2                                                          The SAS System                         07:42 Wednesday, November 24, 2021

38         %LET _CLIENTTASKLABEL=;
39         %LET _CLIENTPROCESSFLOWNAME=;
40         %LET _CLIENTPROJECTPATH=;
41         %LET _CLIENTPROJECTPATHHOST=;
42         %LET _CLIENTPROJECTNAME=;
43         %LET _SASPROGRAMFILE=;
44         %LET _SASPROGRAMFILEHOST=;
45         
46         ;*';*";*/;quit;run;
47         ODS _ALL_ CLOSE;
48         
49         
50         QUIT; RUN;
51         

 

SASKiwi
PROC Star

Yes the filename must match exactly, but why didn't you just try it?

ChrisNZ
Tourmaline | Level 20

1. why a new thread?

2. we can't see if the files were deleted from the log

3. you state you delete CSV and XLS files, but you point to /usr/local/SAS/SASUsers/LabRet/UserDir/RRR/TYOTA  which is neither

 

Kurt_Bremser
Super User
  • I merged the two threads dealing with basically the same issue
  • You cannot remove a non-empty directory with the FDELETE function

So you either have to use an external command (STRONGLY recommended), or iterate recursively through all subdirectories, to clean up all the "leaves" before removing a "branch".

 

@Tom has posted (multiple times) a data step that uses MODIFY to place a directory tree recursively in a dataset; from such a dataset, you can start to first remove the entries with the most separators, down to that with the least, which will be the root of the tree you want to remove.

 

On a UNIX system, the external command is

rm -rf directory

so you can see that not using that and instead coding something like hundred lines is utter stupidity at best. Maxim 14 at work.

 

Ronein
Meteorite | Level 14

Thank you.

I am trying to run the code but I think that I didn't write it correctly.

May you please help?

The Folder is called TYOTA

The path where the folder is located is   "/usr/local/SAS/SASUsers/LabRet/UserDir/RRR/TYOTA"

 

I run this code and get errors

%let path = %nrstr("/usr/local/SAS/SASUsers/LabRet/UserDir/RRR/TYOTA");
options noxwait ;
x "rmdir "/usr/local/SAS/SASUsers/LabRet/UserDir/RRR/" /s /q";
options xwait;
Ksharp
Super User
/*Firstly delete all the files under a fold*/
data have;
rc=filename('xx','/data/sas_data/work/');
did=dopen('xx');
do i=1 to dnum(did);
 fname=dread(did,i);
 output;
end;
rc=dclose(did);
run;
%macro dfile(fname=);
data _null_;
rc=filename('temp',"/data/sas_data/work/&fname");
if rc=0 and fexist('temp') then rc=fdelete('temp');
rc=filename('temp');
put _all_;
run;
%mend;
data _null_;
 set have;
 call execute(cats('%dfile(fname=',fname,')'));
run;








/*Secondly, delete this empty fold*/
%macro dir(fname=);
filename x "/data/sas_data/work/&fname";
data _null_;
 rc=fdelete('x');
 put rc=;
 msg=sysmsg();
 put msg=;
run;
%mend;
data _null_;
 set have;
 call execute(cats('%dir(fname=',fname,')'));
run;



Ronein
Meteorite | Level 14

You are the best!

Thank you

I have run it and it worked 100%

/*Step1-Firstly delete all the files under a fold TYOTA*/
/*Step1-Firstly delete all the files under a fold TYOTA*/
/*Step1-Firstly delete all the files under a fold TYOTA*/
/***Here can see all file names in this folder TYOTA***/
data have;
rc=filename('xx',"/usr/local/SAS/SASUsers/LabRet/UserDir/RRR/TYOTA");
did=dopen('xx');
do i=1 to dnum(did);
 fname=dread(did,i);
 output;
end;
rc=dclose(did);
run;
/***Macro to delete a file located in Folder TYOTA**/
%macro dfile(fname=);
data _null_;
rc=filename('temp',"/usr/local/SAS/SASUsers/LabRet/UserDir/RRR/TYOTA/&fname");
if rc=0 and fexist('temp') then rc=fdelete('temp');
rc=filename('temp');
put _all_;
run;
%mend;
/***Now delete all files that their names are in data set have that we created before***/
data _null_;
 set have;
 call execute(cats('%dfile(fname=',fname,')'));
run;
/*Step2-Deleteelete this empty fold*/
/*Step2-Deleteelete this empty fold*/
/*Step2-Deleteelete this empty fold*/
%macro dir(fname=);
filename x "/usr/local/SAS/SASUsers/LabRet/UserDir/udclk79/TYOTA/&fname";
data _null_;
 rc=fdelete('x');
 put rc=;
 msg=sysmsg();
 put msg=;
run;
%mend;
data _null_;
 set have;
 call execute(cats('%dir(fname=',fname,')'));
run;
AllanBowe
Barite | Level 11

If you'd like to do this recursively (ie, to include subdirectories) you can try this macro: https://core.sasjs.io/mp__deletefolder_8sas.html

 

Example usage:

%let rootdir=%sysfunc(pathname(work))/demo;
%mf_mkdir(&rootdir)
%mf_mkdir(&rootdir/subdir)
%mf_mkdir(&rootdir/subdir/subsubdir)
data "&rootdir/subdir/example.sas7bdat";
run;

%mp_deletefolder(&rootdir)
/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 10 replies
  • 7055 views
  • 9 likes
  • 6 in conversation