相关文章推荐
细心的豌豆  ·  GitHub - ...·  3 月前    · 
胆小的冰淇淋  ·  Python - ...·  11 月前    · 

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);
scan(lowcase(fname),2,'.') in ('xls','xlsx') 
then do;
 rcref = filename('fref',catx('\',"&path.",fname));
 rcdel = fdelete('fref');
 memcount+-1;
stop;
						

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');

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');

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.

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

Register now!

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:

  • Arlington, VA
  • Cary, NC
  • View all other training opportunities.