BookmarkSubscribeRSS Feed
elwayfan446
Barite | Level 11

I have it working for one file.

 

/* THIS WORKS ON ONE FILE */
DATA _NULL_ ;
  rc = filename('fname',"/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/202011_All_Western_Mortgage__Inc..xlsx");
  rc = fdelete('fname');	
RUN ;

I have tried to modify it to loop through all of the files in the directory with no luck.

DATA _NULL_ ;
  rc = filename('fpath',"/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/");
  	did=dopen('fpath');
  	do i=1 to dnum(did);
	  file_name=dread(did,i);
	  /*put file_name;*/
  		rc = filename('fname',catx('fpath',file_name));
		put fname;
  		/*rc = fdelete('fname');*/	
	end;
RUN ;

This block of code gives me these results when I put to the log:

elwayfan446_0-1608679772872.png

I appreciate everyone wanting me to figure this one out on my own, however, I have spent several days on this and need to figure this out to complete a project for my team.  If you could just show me and example on how I need to change my code I would greatly appreciate it.

SASKiwi
PROC Star

File_name is the name of your file not FNAME - there is no FNAME variable. Try this, if RC = 0  - the FILENAME function is successful - then the DELETE is done.

DATA _NULL_ ;
  rc = filename('fpath',"/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/");
  	did=dopen('fpath');
  	do i=1 to dnum(did);
	  file_name=dread(did,i);
	  /*put file_name;*/
  		rc = filename('fname',catx('fpath',file_name));
		put file_name = rc = ;
  		if rc = 0 then rc = fdelete('fname');	
	end;
RUN ;
elwayfan446
Barite | Level 11

@SASKiwi This code ran successfully but the files were not deleted.  Here is the log.

 

elwayfan446_0-1608732318743.png

 

Files still out there:

 

elwayfan446_1-1608732372857.png

 

I am starting to wonder if there is something wrong with the actual filepath or file names that are preventing these types of mass delete since x commands are only working on hard coded files too.  So frustrating.

 

elwayfan446
Barite | Level 11

@SASKiwi, I finally got it to work with a little more trial and error on your code!  Here was the final script:

 

DATA _NULL_ ;
  rc = filename('fpath',"/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/");
  	did=dopen('fpath');
  	do i=1 to dnum(did);
	  file_name=dread(did,i);
  		rc = filename('fname',cats(pathname('fpath'),'/',file_name));
  		if rc = 0 then rc = fdelete('fname');
	end;
RUN ;

I think the problem was the slash between the pathname and filename in the cats function.

 

I want to thank everyone who has been helping me with this the past couple of days.  It has been a big challenge but one that puts me closer to finishing this project and making my boss happy! 😁

ChrisNZ
Tourmaline | Level 20
rc = filename('fname',catx('fpath',file_name));

is wrong. FPATH is not the path. 'FPATH' even less.

Also, function catx is used wrongly.

Maybe this?

RC = filename('FNAME',cats(pathname('FPATH'),FILE_NAME));	

Use might need a slash separator.

 

elwayfan446
Barite | Level 11

Thansk @ChrisNZ.  I tried this adjustment as well.  Same results as @SASKiwi's script.

Tom
Super User Tom
Super User

First make sure you can generate the file names.

Test the return codes from your function calls and emit appropriate error messages.

%let folder=/sae/aal_labapm/01/mortgage/msracq/DATA WAREHOUSE/DWACQ/REPORT FILES/MSR0028/;

data _null_ ;
  length filename $256 ;
  rc = filename('fpath',"&folder.");
  if rc then put "ERROR: Unable to create fileref FPATH pointing to &folder.. " rc=;
  else do;
  	did=dopen('fpath');
    if not did then put "ERROR: Unable to open &folder. as a directory.";
    else do i=1 to dnum(did);
      filename=catx('/',"&folder.",dread(did,i));
      rc = filename('fname',filename);
      if rc then put "ERROR: Unable to create fileref FNAME pointing to " filename :$quote. '. ' rc=;
      else do;
        rc = fdelete('fname');
        if rc then put "ERROR: Unable to delete " filename :$quote. '. ' rc=;
      end;
    end;
  end;
run;
elwayfan446
Barite | Level 11

Thanks @Tom.  I finally figured it out this morning and posted it a few replies above here.  I couldn't figure out how to mark it or yours as the answer because that button is not showing up anywhere for me like it usually does.

 

Thanks for all of your help.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 22 replies
  • 7987 views
  • 13 likes
  • 8 in conversation