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

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

1 REPLY 1
ballardw
Super User

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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 1 reply
  • 4667 views
  • 1 like
  • 2 in conversation