- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have one macro &del_file which has values like file1,file2,file3 . I am trying to write a code to delete these files providing macro name in the code for deleting files.
TIA
Kajal
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@kajal_30 wrote:
I have one macro &del_file which has values like file1,file2,file3 . I am trying to write a code to delete these files providing macro name in the code for deleting files.
TIA
Kajal
So you have a list of filenames in a macro variable.
%let del_file=file1.txt file2.csv file3.exe ;
Note that if the names are not fully qualified then you also need to know the path to the directory where they exist.
Let's just use the first example from the documentation for the FDELETE() function.
So first let's make some example files to delete:
%let del_file=file1.txt file2.csv file3.exe ;
%let path=%sysfunc(pathname(work))/;
data _null_;
length filename $256 ;
do index=1 to countw(symget('del_file'),' ','mq');
filename=cats(symget('path'),scan(symget('del_file'),index,' ','mq'));
file dummy filevar=filename;
put 'xxx';
end;
run;
No let's run a data step to point a fileref at each file and use FDELETE() to remove it. Let's add a non-existent file to the list to make sure we can detect that.
%let del_file=&del_file nosuchfile;
data _null_;
length fileref $8 filename $256 ;
fileref="tempfile";
do index=1 to countw(symget('del_file'),' ','mq');
filename=cats(symget('path'),scan(symget('del_file'),index,' ','mq'));
rc=filename(fileref,filename);
if rc = 0 and fexist(fileref) then do;
rc=fdelete(fileref);
put 'Deleted ' filename= :$quote. rc= ;
end;
else do;
put 'Could not find ' filename= :$quote. rc= ;
end;
rc=filename(fileref);
end;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What have you tried? Do you have a macro or macro variable? If you already have macro code can you share it?
Here's a macro that shows you to delete a single file.
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n02xowj8yuqfo4n0zzi98shu8qup.htm
If you have a list of files to delete in a data set rather than macro variable, it's trivial to loop through and either use this macro + CALL EXECUTE() or just FEXIST()+FDELETE()
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%macro check(del_file);
%if %sysfunc(fileexist(&del_file)) ge 1 %then %do;
%let rc=%sysfunc(filename(temp,&del_file));
%let rc=%sysfunc(fdelete(&temp));
%end;
%else %put The file &file does not exist;
%mend check;
%check(/user/rca/temp1.sas7bndx);
so I am looking for a code which can delete multiple files . I have a macro del_file which has list of files
%let del_file = temp1.sas7bndx,temp2.sas7bndx,temp3.sas7bndx,temp4.sas7bndx
which I want to delete from /user/rca/. Also this location has more files but I just want to delete selected ones.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data test;
infile datalines;
input file_nm $50.;
datalines;
temp1.sas7bndx
temp2.sas7bndx
temp3.sas7bndx
temp4.sas7bndx
temp5.sas7bndx
temp6.sas7bndx
;
run;
data _null_;
set test;
call execute ('%nrstr (%%)macro check;
%if %sysfunc(fileexist(file_nm)) ge 1 %then %do;
%let rc=%sysfunc(filename(temp,file_nm));
%let rc=%sysfunc(fdelete(&temp));
%end;
%else %put The file &file does not exist;
%nrstr(%%)mend;');
run;
%check
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You don’t put the macro definition in CALL Execute just the macro call, ie
%check(filename);
See the second link below OR in the Macro Appendix see the example to loop over a macro list and you can combine the two ideas.
UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
Examples of common macro usage
https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
So you want to remove index files, the sas docs say:
Removing the index file can damage your SAS data set. Also, do not change its name or move it to a different directory. Use the DATASETS procedure to manage indexes. An index file ends with the extension .sas7bndx.
Using proc datasets is recommended.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
what if the files are like test.csv or test.xlsx . can we not handle them using index? my intension is to delete few files from the folder really doesn't matter if I am providing the list of files in a macro or dataset. Also no where I can see this kind a macro
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@kajal_30 wrote:
what if the files are like test.csv or test.xlsx . can we not handle them using index? my intension is to delete few files from the folder really doesn't matter if I am providing the list of files in a macro or dataset. Also no where I can see this kind a macro
Sorry, but i don't understand what you want to say.
If you want to delete non-sas-files, using fdelete function is the best choice if you don't want or can't use os commands. BUT if you want to delete sas-files (dataset, index-files, catalogs etc), then using proc datasets is recommended to avoid errors and unusable datasets.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@kajal_30 wrote:
doesn't matter if I am providing the list of files in a macro or dataset.
Of course it matters, it matters to how the program is written and designed. You also need to provide a full file path, not just the file name.
We've shown you code to delete one file. To delete a list of files is easy, you call the macro multiple times. Exactly how you do that depends on how the list is stored. You started trying that but put the macro code in CALL EXECUTE instead of the macro call.
Your example is slightly confusing because Index files are typically ones you would not want to delete, but I'll assume you understand that if you delete indexes you lose efficiency in accessing files.
*list of files to take an action on;
data test;
infile datalines;
input file_nm $50.;
datalines;
/home/users/temp1.sas7bndx
/home/users/temp2.sas7bndx
/home/users/temp3.sas7bndx
/home/users/temp4.sas7bndx
/home/users/temp5.sas7bndx
/home/users/temp6.sas7bndx
;
run;
*action to do per file - delete file in this case;
%macro check(del_file);
%if %sysfunc(fileexist(&del_file)) ge 1 %then %do;
%let rc=%sysfunc(filename(temp, &del_file));
%let rc=%sysfunc(fdelete(&temp));
%end;
%else %put The file &file does not exist;
%mend check;
*call macro for each file;
data _null_;
set test;
str = catt('%check(', file_nm, ');');
call execute(str);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This macro will delete a folder and recursively, all content within it: https://core.sasjs.io/mp__deletefolder_8sas.html
No need for XCMD
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@AllanBowe I don't want to delete the whole directory but few selected files from the directory .
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@kajal_30 wrote:
I have one macro &del_file which has values like file1,file2,file3 . I am trying to write a code to delete these files providing macro name in the code for deleting files.
TIA
Kajal
So you have a list of filenames in a macro variable.
%let del_file=file1.txt file2.csv file3.exe ;
Note that if the names are not fully qualified then you also need to know the path to the directory where they exist.
Let's just use the first example from the documentation for the FDELETE() function.
So first let's make some example files to delete:
%let del_file=file1.txt file2.csv file3.exe ;
%let path=%sysfunc(pathname(work))/;
data _null_;
length filename $256 ;
do index=1 to countw(symget('del_file'),' ','mq');
filename=cats(symget('path'),scan(symget('del_file'),index,' ','mq'));
file dummy filevar=filename;
put 'xxx';
end;
run;
No let's run a data step to point a fileref at each file and use FDELETE() to remove it. Let's add a non-existent file to the list to make sure we can detect that.
%let del_file=&del_file nosuchfile;
data _null_;
length fileref $8 filename $256 ;
fileref="tempfile";
do index=1 to countw(symget('del_file'),' ','mq');
filename=cats(symget('path'),scan(symget('del_file'),index,' ','mq'));
rc=filename(fileref,filename);
if rc = 0 and fexist(fileref) then do;
rc=fdelete(fileref);
put 'Deleted ' filename= :$quote. rc= ;
end;
else do;
put 'Could not find ' filename= :$quote. rc= ;
end;
rc=filename(fileref);
end;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content