Hi,
I am having problems trying to edit some code to delete specific excel files from a folder. I have a list of filenames and extensions in a table already in SAS, so I just need the code to find those filenames and delete them. I found the below code online and it does delete all the .xlsx .xls files in a folder. However I don't want to delete all files, only the ones within my dataset work.FileN.
Code:
%let path=D:\temp;
filename filrf "&path.";
data test;
/*Set work.FileN; This holds all the filenames and extensions e.g P1.xlsx, P2.xlsx etc.. in column called filename */
did = dopen('filrf');
memcount = dnum(did);
do while (memcount>0);
fname = dread(did,memcount);
if
scan(lowcase(fname),2,'.') in ('xls','xlsx')
then do;
rcref = filename('fref',catx('\',"&path.",fname));
rcdel = fdelete('fref');
end;
memcount+-1;
end;
stop;
run;
If you have the name and path of the files then basically you use your existing SAS data set and modify
rcref = filename('fref',catx('\',"&path.",fname)); rcdel = fdelete('fref');
to use the information in your data set to create the file name replacing the information in
,"&path.",fname with the variables you have in your data set.
All the code in the example from the
did = dopen('filrf');
to
fname = dread(did,memcount);
basically only is getting the names of files in the folder provided by the &path variable.
So since you know what you want you don't need to search and go directly to the delete portion.
If you are implying that you do not know the folder they should be in, then that is another issue entirely.
It almost always helps to provide examples of your existing data so we don't have to guess as to what you might have.
Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
If you have the name and path of the files then basically you use your existing SAS data set and modify
rcref = filename('fref',catx('\',"&path.",fname)); rcdel = fdelete('fref');
to use the information in your data set to create the file name replacing the information in
,"&path.",fname with the variables you have in your data set.
All the code in the example from the
did = dopen('filrf');
to
fname = dread(did,memcount);
basically only is getting the names of files in the folder provided by the &path variable.
So since you know what you want you don't need to search and go directly to the delete portion.
If you are implying that you do not know the folder they should be in, then that is another issue entirely.
It almost always helps to provide examples of your existing data so we don't have to guess as to what you might have.
Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.