DATA Step, Macro, Functions and more

How to check if a folder is empty

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

How to check if a folder is empty

Hi,

 

I need the code to check whether the directory is empty in sas macro.


Accepted Solutions
Solution
‎09-07-2017 07:59 AM
Valued Guide
Posts: 554

Re: How to check if a folder is empty

This should work

 

%macro isemptyfolder(folder);

	%let filrf=mydir;
	%let rc=%sysfunc(filename(filrf, "&folder"));
	%let did=%sysfunc(dopen(&filrf));
	%let memcount=%sysfunc(dnum(&did));
	%let rc=%sysfunc(dclose(&did));
	%put 'number of members in folder ' &folder ' = ' &memcount;

%mend isemptyfolder;

View solution in original post


All Replies
Super User
Super User
Posts: 9,193

Re: How to check if a folder is empty

Please note this is a Q&A board, not a contract work request system.  If you have a question, provide what you have done, show example code using the {i}.  If you have access to the command line then doing a filename tmp pipe 'dir c:\test /b'; and using that in a datastep then checking that might work.  Not sure what the benefit of checking for an empty directory is though, doesn't really add anything.  Do you want to check existence of the folder, or maybe a file you don't want to overwrite?

 

Contributor
Posts: 21

Re: How to check if a folder is empty

Hi @RW9: Thanks for your reply. Just for the info, I am not actually doing any project but making myself more familiar with macro programming. I was actually writing a UNIX shell script which had certain validations and thought for the same in SAS.
Solution
‎09-07-2017 07:59 AM
Valued Guide
Posts: 554

Re: How to check if a folder is empty

This should work

 

%macro isemptyfolder(folder);

	%let filrf=mydir;
	%let rc=%sysfunc(filename(filrf, "&folder"));
	%let did=%sysfunc(dopen(&filrf));
	%let memcount=%sysfunc(dnum(&did));
	%let rc=%sysfunc(dclose(&did));
	%put 'number of members in folder ' &folder ' = ' &memcount;

%mend isemptyfolder;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 395 views
  • 3 likes
  • 3 in conversation