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

I have one macro &del_file which has values like file1,file2,file3 . I am trying to write a code to delete these files providing macro name in the code for deleting files.

TIA

Kajal 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@kajal_30 wrote:

I have one macro &del_file which has values like file1,file2,file3 . I am trying to write a code to delete these files providing macro name in the code for deleting files.

TIA

Kajal 


So you have a list of filenames in a macro variable. 

%let del_file=file1.txt file2.csv file3.exe ;

Note that if the names are not fully qualified then you also need to know the path to the directory where they exist.

Let's just use the first example from the documentation for the FDELETE() function.  

So first let's make some example files to delete:

%let del_file=file1.txt file2.csv file3.exe ;
%let path=%sysfunc(pathname(work))/;

data _null_;
  length filename $256 ;
  do index=1 to countw(symget('del_file'),' ','mq');
    filename=cats(symget('path'),scan(symget('del_file'),index,' ','mq'));
    file dummy filevar=filename;
    put 'xxx';
  end;
run;

No let's run a data step to point a fileref at each file and use FDELETE() to remove it.  Let's add a non-existent file to the list to make sure we can detect that.

%let del_file=&del_file nosuchfile;
data _null_;
  length fileref $8 filename $256 ;
  fileref="tempfile";
  do index=1 to countw(symget('del_file'),' ','mq');
    filename=cats(symget('path'),scan(symget('del_file'),index,' ','mq'));
    rc=filename(fileref,filename);
    if rc = 0 and fexist(fileref) then do;
       rc=fdelete(fileref);
       put 'Deleted ' filename= :$quote. rc= ;
    end;
    else do;
       put 'Could not find ' filename= :$quote. rc= ;
    end;
    rc=filename(fileref);
  end;
run;

 

 

View solution in original post

14 REPLIES 14
Reeza
Super User
So what is your question?
What have you tried? Do you have a macro or macro variable? If you already have macro code can you share it?

Here's a macro that shows you to delete a single file.
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n02xowj8yuqfo4n0zzi98shu8qup.htm

If you have a list of files to delete in a data set rather than macro variable, it's trivial to loop through and either use this macro + CALL EXECUTE() or just FEXIST()+FDELETE()
kajal_30
Quartz | Level 8

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

%check(/user/rca/temp1.sas7bndx);

so I am looking for  a code which can delete multiple files . I have a macro del_file which has list of files 

%let del_file = temp1.sas7bndx,temp2.sas7bndx,temp3.sas7bndx,temp4.sas7bndx

which I want to delete from /user/rca/. Also this location has more files but I just want to delete selected ones. 

kajal_30
Quartz | Level 8


data test;
infile datalines;
input file_nm $50.;
datalines;
temp1.sas7bndx
temp2.sas7bndx
temp3.sas7bndx
temp4.sas7bndx
temp5.sas7bndx
temp6.sas7bndx
;
run;


data _null_;
set test;
call execute ('%nrstr (%%)macro check;
%if %sysfunc(fileexist(file_nm)) ge 1 %then %do;
%let rc=%sysfunc(filename(temp,file_nm));
%let rc=%sysfunc(fdelete(&temp));
%end;
%else %put The file &file does not exist;
%nrstr(%%)mend;');
run;
%check

Reeza
Super User

You don’t put the macro definition in CALL Execute just the macro call, ie 

 

%check(filename);

 

See the second link below OR in the Macro Appendix see the example to loop over a macro list and you can combine the two ideas. 


UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

Examples of common macro usage

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

andreas_lds
Jade | Level 19

So you want to remove index files, the sas docs say:

Removing the index file can damage your SAS data set. Also, do not change its name or move it to a different directory. Use the DATASETS procedure to manage indexes. An index file ends with the extension .sas7bndx.

Using proc datasets is recommended.

kajal_30
Quartz | Level 8

what if the files are like test.csv or test.xlsx . can we not handle them using index? my intension is to delete few files from the folder really doesn't matter if I am providing the list of files in a macro or dataset. Also no where I can see this kind a macro  

andreas_lds
Jade | Level 19

@kajal_30 wrote:

what if the files are like test.csv or test.xlsx . can we not handle them using index? my intension is to delete few files from the folder really doesn't matter if I am providing the list of files in a macro or dataset. Also no where I can see this kind a macro  


Sorry, but i don't understand what you want to say.

If you want to delete non-sas-files, using fdelete function is the best choice if you don't want or can't use os commands. BUT if you want to delete sas-files (dataset, index-files, catalogs etc), then using proc datasets is recommended to avoid errors and unusable datasets.

Reeza
Super User

@kajal_30 wrote:

doesn't matter if I am providing the list of files in a macro or dataset. 


Of course it matters, it matters to how the program is written and designed. You also need to provide a full file path, not just the file name.

We've shown you code to delete one file. To delete a list of files is easy, you call the macro multiple times. Exactly how you do that depends on how the list is stored. You started trying that but put the macro code in CALL EXECUTE instead of the macro call.

 

Your example is slightly confusing because Index files are typically ones you would not want to delete, but I'll assume you understand that if you delete indexes you lose efficiency in accessing files. 

 

*list of files to take an action on;
data test;
infile datalines;
input file_nm $50.;
datalines;
/home/users/temp1.sas7bndx
/home/users/temp2.sas7bndx
/home/users/temp3.sas7bndx
/home/users/temp4.sas7bndx
/home/users/temp5.sas7bndx
/home/users/temp6.sas7bndx
;
run;

*action to do per file - delete file in this case;
%macro check(del_file);
%if %sysfunc(fileexist(&del_file)) ge 1 %then %do;
%let rc=%sysfunc(filename(temp, &del_file));
%let rc=%sysfunc(fdelete(&temp));
%end;
%else %put The file &file does not exist;
%mend check;

*call macro for each file;
data _null_;
set test;

str = catt('%check(', file_nm, ');');

call execute(str);

run;

 

 

 

AllanBowe
Barite | Level 11

 This macro will delete a folder and recursively, all content within it:  https://core.sasjs.io/mp__deletefolder_8sas.html

No need for XCMD

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
kajal_30
Quartz | Level 8

@AllanBowe I don't want to delete the whole directory but few selected files from the directory .

 

Tom
Super User Tom
Super User

@kajal_30 wrote:

I have one macro &del_file which has values like file1,file2,file3 . I am trying to write a code to delete these files providing macro name in the code for deleting files.

TIA

Kajal 


So you have a list of filenames in a macro variable. 

%let del_file=file1.txt file2.csv file3.exe ;

Note that if the names are not fully qualified then you also need to know the path to the directory where they exist.

Let's just use the first example from the documentation for the FDELETE() function.  

So first let's make some example files to delete:

%let del_file=file1.txt file2.csv file3.exe ;
%let path=%sysfunc(pathname(work))/;

data _null_;
  length filename $256 ;
  do index=1 to countw(symget('del_file'),' ','mq');
    filename=cats(symget('path'),scan(symget('del_file'),index,' ','mq'));
    file dummy filevar=filename;
    put 'xxx';
  end;
run;

No let's run a data step to point a fileref at each file and use FDELETE() to remove it.  Let's add a non-existent file to the list to make sure we can detect that.

%let del_file=&del_file nosuchfile;
data _null_;
  length fileref $8 filename $256 ;
  fileref="tempfile";
  do index=1 to countw(symget('del_file'),' ','mq');
    filename=cats(symget('path'),scan(symget('del_file'),index,' ','mq'));
    rc=filename(fileref,filename);
    if rc = 0 and fexist(fileref) then do;
       rc=fdelete(fileref);
       put 'Deleted ' filename= :$quote. rc= ;
    end;
    else do;
       put 'Could not find ' filename= :$quote. rc= ;
    end;
    rc=filename(fileref);
  end;
run;

 

 

kajal_30
Quartz | Level 8
Thank you so much for this. may I know what this 'mq' represents here
Tom
Super User Tom
Super User

@kajal_30 wrote:
Thank you so much for this. may I know what this 'mq' represents here

Read the documentation:

COUNTW()

SCAN()

kajal_30
Quartz | Level 8
thank you again macro is working fine for deleting specified files but is there a way we can let this macro to delete files like file1.csv , file1.pdf , file1.xlsx and more formats which we are not aware of ? we just have the file name as file1 but not sure about the extension . I tried putting some linux options but didn't work

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
  • 14 replies
  • 1044 views
  • 4 likes
  • 5 in conversation