BookmarkSubscribeRSS Feed
Citrine10
Obsidian | Level 7

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;
5 REPLIES 5
LinusH
Tourmaline | Level 20

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.

Data never sleeps
Citrine10
Obsidian | Level 7
What i mean is when I use my delete macro with the full path, the file deletes successfully 🙂
Quentin
Super User

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.

ballardw
Super User

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.

Tom
Super User Tom
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1857 views
  • 0 likes
  • 5 in conversation