Hi I am trying to delete a file by passing the filename in a variable. When using my delete macro the file deletes if I put the name there however when using my variable it doesnt work
%include "C:/list.sas"; /*file paths*/ %let path_in= C:/FileCheck; /*List of files*/ %list_files("C:/FileCheck",txt) proc sql; create table filecount as select name as filename from tables (obs = 1); quit; %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; data tsave; length y fname $200; do i = 1 to 3; y = cats("C:/","&filename"); fname = y; output; file dummy filevar=y; %check('&y') end; run;
Can you elaborate a bit about "if I put the name there".
Also, if you could post any logs or anything can help us understand what happens in both scenarios.
You are trying to use data from the DATA step (PDV) to generate macro calls. For that, look into examples of people using CALL EXECUTE to generate macro calls.
As written, the code can't work because the macro %check executes before the data step with the DO loop has even finished compiling.
Sorry I don't have time to give you a working example, I'm sure someone else will likely write a CALL EXECUTE solution for you.
Not macro needed. Please note that the key part of your "macro" is this
fileexist(&file)
FILEEXIST is a data step function. Which is why the %sysfunc is needed to execute it in a macro. As is FDELETE.
Place the names in a data set and just use the Fileexist and Fdelete in data step code with the variable name holding the values.
It doesn't help that you 1) pass the value in the macro in quotes and 2) the quotes are single quotes preventing resolution of the macro variable which would 3) make the quote part of the value which would not be needed/wanted in the macro itself.
So let's assume that your first macro will create a dataset named TABLES with a variable named NAME hat has the name of a file without the path to the file.
You can then use a simple data step to use that dataset. Add back the path so that you can then delete the files.
data tsave;
set tables;
length fullname $256 fileref $8 ;
fullname = catx('/',"c:\FileCheck",name);
if fileexist(fullname) then do;
rc=filename(fileref,fullname);
if rc then put 'WARNING: Unable to create fileref for ' fullname= ;
else do;
rc=fdelete(fileref);
if rc then put 'ERROR: Unable to delete ' fullname= ;
end;
end;
else put 'WARNING: Unable to find ' fullname=;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.