I am dealing with some files that need to be FDELETED after they are used. However, I only get a log message:
ERROR: Invalid physical name.
I've discovered I can used brackets in some scenarios by using backslash (\[)...\]) to apparently escape the [ which causes the above error.
I recall reading some SAS documentation that [] in filename is used like a regex character class, but I can't find that doc again -- it might have been Viya related, but I am not using Viya.
Example:
%let workpath = %sysfunc(pathname(WORK)) ;
%let testpath = &workpath/test ;
options dlcreatedir ;
libname testpath "&testpath" ;
options nodlcreatedir ;
* Create a test file whose name contains brackets ;
filename bracket "&testpath/ABC_\[123\]_DEF.txt" ;
ata _null_;
file bracket;
put 'Where do you want these shelves?';
run ;
* Process the test file ;
data _null_ ;
infile bracket ;
input; put _infile_;
run;
* List the files in test folder and try to delete them with FDELETE ;
data _null_ ;
length dref fref $8 fname $256 msg $200 ;
rc = filename (dref, "&testpath") ;
did = dopen(dref); msg=sysmsg() ;
do i = 1 to dnum(did) ;
fname = dread(did, i) ;
put 'NOTE: ' i fname ;
rc = filename (fref, cats("&testpath",'/',fname)) ; msg = sysmsg() ; put 'NOTE: filename() ' rc= fname= / msg ;
rc = FDELETE(fref) ; msg = sysmsg() ; put 'NOTE: fdelete() ' rc= / msg ;
if index(fname,'[') then do ;
fname = transtrn(fname,'[','\[') ; * try to escape the brackets ;
fname = transtrn(fname,']','\]') ;
rc = filename (fref, cats("&testpath",'/',fname)) ; msg = sysmsg() ; put 'NOTE: filename() ' rc= fname= / msg ;
rc = FDELETE(fref) ; msg = sysmsg() ; put 'NOTE: fdelete() ' rc= / msg ;
end ;
rc = filename (fref) ;
end ;
run ;
* try delete the escaped filename referenced by bracket;
%put NOTE: FDELETE rc=%sysfunc(fdelete(bracket)) ;
%put %sysfunc(sysmsg()) ;
will log the ERROR: for each way I try to FDELETE
225 %let workpath = %sysfunc(pathname(WORK)) ;
226 %let testpath = &workpath/test ;
227
228 options dlcreatedir ;
229 libname testpath "&testpath" ;
NOTE: Libref TESTPATH was successfully assigned as follows:
Engine: V9
Physical Name: /saswork/sas_tmp/SAS_work##########/SAS_work##########/test
230 options nodlcreatedir ;
231
232 filename bracket "&testpath/ABC_\[123\]_DEF.txt" ;
233
234 data _null_;
235 file bracket;
236 put 'Where do you want these shelves?';
237 run ;
NOTE: The file BRACKET is:
Filename=/saswork/sas_tmp/SAS_work##########/SAS_work##########/test/ABC_\[123\]_DEF.txt,
Owner Name=########,Group Name=########,
Access Permission=-rw-rw-rw-,
Last Modified=17Jul2025:08:27:44
NOTE: 1 record was written to the file BRACKET.
The minimum record length was 32.
The maximum record length was 32.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
238
239 data _null_ ;
240 infile bracket ;
241 input; put _infile_;
242 run;
NOTE: The infile BRACKET is:
Filename=/saswork/sas_tmp/SAS_work##########/SAS_work##########/test/ABC_\[123\]_DEF.txt,
Owner Name=########,Group Name=########,
Access Permission=-rw-rw-rw-,
Last Modified=17Jul2025:08:27:44,
File Size (bytes)=33
Where do you want these shelves?
NOTE: 1 record was read from the infile BRACKET.
The minimum record length was 32.
The maximum record length was 32.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
243
244 * List the files in test folder and try to delete them ;
245
246 data _null_ ;
247 length dref fref $8 fname $256 msg $200 ;
248 rc = filename (dref, "&testpath") ;
249 did = dopen(dref); msg=sysmsg() ;
250 do i = 1 to dnum(did) ;
251 fname = dread(did, i) ;
252 put 'NOTE: ' i fname ;
253 rc = filename (fref, cats("&testpath",'/',fname)) ; msg = sysmsg() ; put 'NOTE: filename() ' rc= fname= / msg ;
254 rc = FDELETE(fref) ; msg = sysmsg() ; put 'NOTE: fdelete() ' rc= / msg ;
255 if index(fname,'[') then do ;
256 fname = transtrn(fname,'[','\[') ;
257 fname = transtrn(fname,']','\]') ;
258 rc = filename (fref, cats("&testpath",'/',fname)) ; msg = sysmsg() ; put 'NOTE: filename() ' rc= fname= / msg ;
259 rc = FDELETE(fref) ; msg = sysmsg() ; put 'NOTE: fdelete() ' rc= / msg ;
260 end ;
261 rc = filename (fref) ;
262 end ;
263 run ;
NOTE: 1 ABC_[123]_DEF.txt
NOTE: filename() rc=0 fname=ABC_[123]_DEF.txt
NOTE: fdelete() rc=20017
ERROR: Invalid physical name.
NOTE: filename() rc=0 fname=ABC_\[123\]_DEF.txt
NOTE: fdelete() rc=20017
ERROR: Invalid physical name.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
264
265 * try delete the escaped filename referenced by bracket;
266
267 %put NOTE: macro FDELETE rc=%sysfunc(fdelete(bracket)) ;
NOTE: macro FDELETE rc=20017
268 %put %sysfunc(sysmsg()) ;
ERROR: Invalid physical name.
... View more