SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
arde
Obsidian | Level 7

Hi,

 

Is there a way to delete certain files from a folder?  I have a conversion program that creates a bunch of csv files.  However, I would like to delete all the files that begin with "DES" since they're not needed.  Is there a step I can add to the conversion program to delete the DES files?

 

Any help is truly appreciated.

 

Thanks

8 REPLIES 8
Reeza
Super User
FDELETE() will delete a file. Without seeing the rest of your conversion program it's hard to say what is possible.
arde
Obsidian | Level 7
Is there a way to say in FDELETE to delete any file that starts with "DES"
Reeza
Super User
You can try a wild card, "DES*.txt" and see what happens. That works in other commands....
arde
Obsidian | Level 7
I'm not familiar with macros or fdelete. But I found this on the sas website and modified it:

%macro check(file);
%if %sysfunc(fileexist(&file)) ge 1 %then %do;
%let rc=%sysfunc(filename(temp,&file));
%let rc=%sysfunc(fdelete(&temp));
%end;
%else %put The file &file does not exist;
%mend check;

%check(C:\Desktop\Excel\DES*.csv)

It didn't work though
Reeza
Super User
1. Lookup the documentation on FDELETE and try deleting a single file
2. Try deleting a series of files using wildcard
3. Once it's working, incorporate it into your code.
SuryaKiran
Meteorite | Level 14

Hello,

 

It depends on what environment your working on. Windows or Unix/Linux. Did you try the X-commands (run system commands from SAS).

In UNIX/LINUX:

X "find -name . 'DES*' -exec rm {} \;";

In Windows:

X "Get-ChildItem -include DES* -recurse -force | Remove-Item -force";

 

In some environments (Unix) SAS users are restricted to run commands, in that case you may try:

/* Read files in a folder */

%let path=/user/files;

FILENAME _folder_ "%bquote(&path.)";
data filenames(keep=memname);
  handle=dopen( '_folder_' );
  if handle > 0 then do;
    count=dnum(handle);
    do i=1 to count;
      memname=dread(handle,i);
      if substr(memname,1,3)='Wra' then output filenames;
    end;
  end;
  rc=dclose(handle);
run;
filename _folder_ clear;



/* delete files identified in above step */
data _null_;
set filenames;
fname = 'todelete';
rc = filename(fname, quote(cats("&path",'/',memname)));
rc = fdelete(fname);
rc = filename(fname);
run;
Thanks,
Suryakiran
Avinash14
Calcite | Level 5
Thanks Suryakiran. Your code works perfectly fine for me.
Reeza
Super User

It looks like FDELETE() does not support wildcards so you're best off using SYSEXEC() and a command instead. That would be like:

Be careful with the quotes there.

 

x 'rm "path to file\DES*.extension"';

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 8 replies
  • 18153 views
  • 4 likes
  • 4 in conversation