I found the code to delete specific type of files using the sas code; just curious can we remove the files from the folder using the CMD codes in SAS?
Reference to remove using SSAS code :https://stackoverflow.com/questions/23160177/sas-delete-all-txt-files-in-a-folder
I suggest you to use code from @Kurt_Bremser's "Talking to Your Host" article from WUSS 2022 (https://communities.sas.com/t5/SAS-User-Groups-Library/WUSS-Presentation-Talking-to-Your-Host/ta-p/8...)
The code would be like this:
/* location of a directory */
%let filesLocation=C:\SAS_WORK\;
/* get full list of files with given extension from &filesLocation. (including all subdirectories) */
data files_to_delete; /* just to be sure the file "files" is "empty" */
run;
data files_to_delete;
length root dname $ 2048 filename $ 256 dir level 8;
root = "&filesLocation.";
retain filename dname ' ' level 0 dir 1;
label
filename = "file"
dname = "folder"
;
run;
data files_to_delete;
modify files_to_delete;
rc1=filename('tmp',catx('/',root,dname,filename));
rc2=dopen('tmp');
dir = 1 & rc2;
if dir then
do;
dname=catx('/',dname,filename);
filename=' ';
end;
replace;
if dir;
level=level+1;
do i=1 to dnum(rc2);
filename=dread(rc2,i);
if upcase(scan(filename,-1,"."))="EXTENSION_TO_DROP" then output; /* filter out data by Extension_TO_DROP */
end;
rc3=dclose(rc2);
run;
/* Use code from the FDELETE() function documentation to Delete Files */
data _null_;
set files_to_delete;
fname="tempfile";
rc=filename(fname, catx('/',root,dname,filename));
if rc = 0 and fexist(fname) then
rc=fdelete(fname);
rc=filename(fname);
run;
All the best
Bart
External commands (through X statement, SYSTEM function, CALL SYSTEM, %SYSEXEC, INFILE PIPE) can only be used if the XCMD system option is set. If NOXCMD is set (default for BI Server installations), you must rely on the SAS functions.
Thank you.
I suggest you to use code from @Kurt_Bremser's "Talking to Your Host" article from WUSS 2022 (https://communities.sas.com/t5/SAS-User-Groups-Library/WUSS-Presentation-Talking-to-Your-Host/ta-p/8...)
The code would be like this:
/* location of a directory */
%let filesLocation=C:\SAS_WORK\;
/* get full list of files with given extension from &filesLocation. (including all subdirectories) */
data files_to_delete; /* just to be sure the file "files" is "empty" */
run;
data files_to_delete;
length root dname $ 2048 filename $ 256 dir level 8;
root = "&filesLocation.";
retain filename dname ' ' level 0 dir 1;
label
filename = "file"
dname = "folder"
;
run;
data files_to_delete;
modify files_to_delete;
rc1=filename('tmp',catx('/',root,dname,filename));
rc2=dopen('tmp');
dir = 1 & rc2;
if dir then
do;
dname=catx('/',dname,filename);
filename=' ';
end;
replace;
if dir;
level=level+1;
do i=1 to dnum(rc2);
filename=dread(rc2,i);
if upcase(scan(filename,-1,"."))="EXTENSION_TO_DROP" then output; /* filter out data by Extension_TO_DROP */
end;
rc3=dclose(rc2);
run;
/* Use code from the FDELETE() function documentation to Delete Files */
data _null_;
set files_to_delete;
fname="tempfile";
rc=filename(fname, catx('/',root,dname,filename));
if rc = 0 and fexist(fname) then
rc=fdelete(fname);
rc=filename(fname);
run;
All the best
Bart
Thank you
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.