BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASuserlot
Barite | Level 11

 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

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

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.

yabwon
Onyx | Level 15

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

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 908 views
  • 2 likes
  • 3 in conversation