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
@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;
%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.
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
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...
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.
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
@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.
@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;
This macro will delete a folder and recursively, all content within it: https://core.sasjs.io/mp__deletefolder_8sas.html
No need for XCMD
@AllanBowe I don't want to delete the whole directory but few selected files from the directory .
@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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.