I have it working for one file.
/* THIS WORKS ON ONE FILE */
DATA _NULL_ ;
rc = filename('fname',"/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/202011_All_Western_Mortgage__Inc..xlsx");
rc = fdelete('fname');
RUN ;
I have tried to modify it to loop through all of the files in the directory with no luck.
DATA _NULL_ ;
rc = filename('fpath',"/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/");
did=dopen('fpath');
do i=1 to dnum(did);
file_name=dread(did,i);
/*put file_name;*/
rc = filename('fname',catx('fpath',file_name));
put fname;
/*rc = fdelete('fname');*/
end;
RUN ;
This block of code gives me these results when I put to the log:
I appreciate everyone wanting me to figure this one out on my own, however, I have spent several days on this and need to figure this out to complete a project for my team. If you could just show me and example on how I need to change my code I would greatly appreciate it.
File_name is the name of your file not FNAME - there is no FNAME variable. Try this, if RC = 0 - the FILENAME function is successful - then the DELETE is done.
DATA _NULL_ ;
rc = filename('fpath',"/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/");
did=dopen('fpath');
do i=1 to dnum(did);
file_name=dread(did,i);
/*put file_name;*/
rc = filename('fname',catx('fpath',file_name));
put file_name = rc = ;
if rc = 0 then rc = fdelete('fname');
end;
RUN ;
@SASKiwi This code ran successfully but the files were not deleted. Here is the log.
Files still out there:
I am starting to wonder if there is something wrong with the actual filepath or file names that are preventing these types of mass delete since x commands are only working on hard coded files too. So frustrating.
@SASKiwi, I finally got it to work with a little more trial and error on your code! Here was the final script:
DATA _NULL_ ;
rc = filename('fpath',"/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/");
did=dopen('fpath');
do i=1 to dnum(did);
file_name=dread(did,i);
rc = filename('fname',cats(pathname('fpath'),'/',file_name));
if rc = 0 then rc = fdelete('fname');
end;
RUN ;
I think the problem was the slash between the pathname and filename in the cats function.
I want to thank everyone who has been helping me with this the past couple of days. It has been a big challenge but one that puts me closer to finishing this project and making my boss happy! 😁
rc = filename('fname',catx('fpath',file_name));
is wrong. FPATH is not the path. 'FPATH' even less.
Also, function catx is used wrongly.
Maybe this?
RC = filename('FNAME',cats(pathname('FPATH'),FILE_NAME));
Use might need a slash separator.
First make sure you can generate the file names.
Test the return codes from your function calls and emit appropriate error messages.
%let folder=/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/;
data _null_ ;
length filename $256 ;
rc = filename('fpath',"&folder.");
if rc then put "ERROR: Unable to create fileref FPATH pointing to &folder.. " rc=;
else do;
did=dopen('fpath');
if not did then put "ERROR: Unable to open &folder. as a directory.";
else do i=1 to dnum(did);
filename=catx('/',"&folder.",dread(did,i));
rc = filename('fname',filename);
if rc then put "ERROR: Unable to create fileref FNAME pointing to " filename :$quote. '. ' rc=;
else do;
rc = fdelete('fname');
if rc then put "ERROR: Unable to delete " filename :$quote. '. ' rc=;
end;
end;
end;
run;
Thanks @Tom. I finally figured it out this morning and posted it a few replies above here. I couldn't figure out how to mark it or yours as the answer because that button is not showing up anywhere for me like it usually does.
Thanks for all of your help.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: