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

Hi all,

 

I am using SAS Studio in order to upload an excel file and import it vía SAS code.

 

Once the file imported, I would like to delete excel file "xxx.xlsx" from the folder "/Users/%trim(&SYS_COMPUTE_SESSION_OWNER)/My Folder/", because if I want to upload another file with the same name, comes into "xxx (1).xlsx" and my code doesnt work.

 

Any ideas to clear the files of myFolder??

 

Thanks a lot

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ
/* /Users/EX392461/My Folder/FTF.xlsx                     */
/* Take care : folderpath and filename are CASE SENSITIVE */

FILENAME abc FILESRVC folderpath='/Users/EX392461/My Folder' filename='FTF.xlsx';
data _null_;
 rc = fdelete('abc');
 rc = filename('abc');
run;
/* end of program */

Koen

 

View solution in original post

15 REPLIES 15
Kurt_Bremser
Super User

Use the FILENAME and FDELETE functions:

data _null_;
rc = filename(fref,"/Users/%trim(&SYS_COMPUTE_SESSION_OWNER)/My Folder/xxx.xlsx");
rc = fdelete(fref);
rc = filename(fref);
run;
sbxkoenk
SAS Super FREQ

Hello,

 

Instead of deleting immediately after import ... it's maybe better to delete before next import (?).

/* macro to Delete a File If It Exists */
%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(/Users/%trim(&SYS_COMPUTE_SESSION_OWNER)/My Folder/xxx.xlsx)

Koen

lekouna
Fluorite | Level 6
It doesn't work.. there is any solutions using %let folder=/Users/%trim(&SYS_COMPUTE_SESSION_OWNER)/My Folder/;
and %let Fichero=xxx.xlsx; becausa as filename doesn't recognize it.. I don't know why!!
lekouna
Fluorite | Level 6

lekouna_0-1688917184657.png

doesn't work... 

using the following it work the import code, but I dont know if there is way to delete the excel file 

%let folder=/Users/%trim(&SYS_COMPUTE_SESSION_OWNER)/My Folder/;

%let Fichero=FTF.xlsx;

 

Tom
Super User Tom
Super User

Why not just use ~ to indicate your home directory?

Make a fileref that points to the file and then use that fileref in both the IMPORT and the FDELETE() steps.

%let folder=~/My Folder;
%let Fichero=FTF.xlsx;
filename xlsx "&folder/&fichero";
proc import datafile=xlsx dbms=xlsx out=WANT replace ;
run;
%put Return Code = %sysfunc(fdelete(xlsx));
filename xlsx ;
sbxkoenk
SAS Super FREQ

Hello,

 

OK, your *.xlsx file is in the SAS Content folder and not in the Linux File System.

A (sub-)folder in the SAS Content folder is a virtual container rather than a representation of a physical file system.

The SAS Content folder is in fact the SAS Infrastructure Data Server which is based on PostgreSQL (a database).

 

Therefore you need the:
FILENAME Statement: FILESRVC Access Method

to gain access.

 

The 1st datastep in the below code works for me, but the 2nd one unfortunately not. 🤔😕
I don't know how to use that FILESRVC Access Method in the FILENAME function.
I know how to use it in the FILENAME statement, but NOT how to use it in the FILENAME function.

 

FILENAME abc FILESRVC folderpath='/Users/sbxkok/My Folder/srclib' filename='dummy.sas';

data work.xyz;
 infile abc;
 input column1 $;
 put _infile_;
run;

data _null_;
 rc = filename(fname,"/Users/sbxkok/My Folder/srclib/dummy.sas",'FILESRVC');
 rc = fdelete(fname);
 rc = filename(fname);
run;
/* end of program */

I guess someone else will chime in to help us out.

 

Koen

 
Tom
Super User Tom
Super User

Why make a new fileref if you already had one?
What happens if you use FDELETE() with ABC?

data _null_;
 rc = fdelete('abc');
 rc = filename('abc');
run;
sbxkoenk
SAS Super FREQ

OK @Tom ... that worked.

 

In my previous attempt I forgot the quotes around abc in the fdelete and filename functions (data _NULL_ step).

80   FILENAME abc FILESRVC folderpath='/Users/sbxkok/My Folder/srclib' filename='dummy.sas';
81   
82   data _null_;
83    rc = fdelete('abc');
84    rc = filename('abc');
85   run;
NOTE: DATA statement used (Total process time):
      real time           0.31 seconds
      cpu time            0.09 seconds

Thanks!

@lekouna --> you now have a working program!

 

Koen

 
lekouna
Fluorite | Level 6
sorry my ignorance, but how could translate it in my case?
sbxkoenk
SAS Super FREQ
/* /Users/EX392461/My Folder/FTF.xlsx                     */
/* Take care : folderpath and filename are CASE SENSITIVE */

FILENAME abc FILESRVC folderpath='/Users/EX392461/My Folder' filename='FTF.xlsx';
data _null_;
 rc = fdelete('abc');
 rc = filename('abc');
run;
/* end of program */

Koen

 
lekouna
Fluorite | Level 6
Thanks a lot! 🙂
that´s the way to delete the xlxs from My Folder
lekouna
Fluorite | Level 6
when I try to put %trim(&SYS_COMPUTE_SESSION_OWNER) to EX392461 there is the ERROR ERROR: HTTP error returned from service.
NOTE: **************** HTTP Request Call ****************
ERROR: The Folders Service request failed with HTTP status 403: Forbidden.
NOTE: The requested Service operation cannot be performed with the provided credentials.
ERROR: An error occurred searching for the specified folder '/Users/%trim(&SYS_COMPUTE_SESSION_OWNER)/My Folder'
ERROR: Error in the FILENAME statement.
Do you have any idea of how to apply it for various users??
sbxkoenk
SAS Super FREQ

Hello,

 

When you use a macro variable (and you want it resolved by prefixing it with &), you should use double quotes ("...").
SAS is not looking inside single quotes ('...'). What is inside single quotes is considered a fixed string (not to be altered).

 

%trim(&SYS_COMPUTE_SESSION_OWNER.) is working for me, but you might need SYSUSERID instead of SYS_COMPUTE_SESSION_OWNER. In many SAS installations, both are the same (but sometimes SYSUSERID equals sassrv).

 

%PUT &=SYS_COMPUTE_SESSION_OWNER;
%PUT &=SYSUSERID;

FILENAME abc FILESRVC folderpath="/Users/%trim(&SYS_COMPUTE_SESSION_OWNER.)/My Folder/srclib" 
                        filename='dummy.sas';
data _null_;
 rc = fdelete('abc');
 rc = filename('abc');
run;
/* end of program */

Koen

 
lekouna
Fluorite | Level 6
Double quotes are work! 😉
Thanks a lot!

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
Develop Code with SAS Studio

Get started using SAS Studio to write, run and debug your SAS programs.

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
  • 15 replies
  • 8618 views
  • 3 likes
  • 4 in conversation