How To List All Files Recursively In SAS?

How to List all files recursively in SAS?

Data analysts often have to deal with large amounts of data stored in a variety of directories and subdirectories. There are several SAS methods for listing all files within a directory, including subdirectories.

One method to list all files within a directory including subdirectories in SAS using data access functions is to use SAS data access functions. These functions can be used to obtain a list of the files in a directory under Windows or Unix. 

data filenames;
	length fref $8 fname $200;
	did=filename(fref, '/home/9tos5as/inputs');
	did=dopen(fref);

	do i=1 to dnum(did);
		fname=dread(did, i);
		output;
	end;
	did=dclose(did);
	did=filename(fref);
	keep fname;
run;
How to List all files recursively in SAS?

This method only lists files and folders in the directory and does not include files in subdirectories.

In this blog post, I am going to introduce you to the list_files macro. This macro helps you to list all the filenames present in a directory, and it can also search for files in its subdirectories. If you need to look for files with a specific extension, don’t worry! This macro can also take care of that.

/* Delete dataset if exists */
proc datasets library=work nolist;
delete allfiles;
quit;
%macro list_files(dir, ext) /parmbuff;
	%put Start&=dir;
	%local sep filrf rc did name i fid;
	%let sep= %sysfunc(ifc(%sysfunc(find(&dir, /)), /, \));
	%let rc=%sysfunc(filename(filrf, &dir));
	%let did=%sysfunc(dopen(&filrf));


	%if &did eq 0 %then
		%do;
			%put Directory &dir cannot be open or does not exist;
			%Put WARNING: not exist &dir.;
			%return;
		%end;

	%do i=1 %to %sysfunc(dnum(&did));
		%let name=%qsysfunc(dread(&did, &i));
		%let fid = %sysfunc(mopen(&did, &name));
		%if &fid ne 0 %then
			%do;
				%if (&ext ne %str() and %qupcase(%qscan(&name, -1, 
					.))=%upcase(&ext)) or &ext=%str() %then
				%do;
					%put &dir&sep&name;
						data files;
						length Directory $200 FileName $200;
							Directory="&dir&sep";
							FileName="&name";
						run;

						proc append base=allfiles data=files;
						run;
				%end;
		%end;
		%else 
			%do;
				%list_files(&dir&sep%unquote(&name), &ext) %end;
			%end;
		%let rc=%sysfunc(dclose(&did));
		%let rc=%sysfunc(filename(filrf));
	%mend list_files;

	%list_files(/home/subhroster20070/sas_inputs);

	data _null_;
		dsid=open('allfiles', 'i');
		n_obs=attrn(dsid, 'nobs');
		n_vars=attrn(dsid, 'nvars');
		dslabel=attrc(dsid, 'label');
		rc=close(dsid);

		if n_obs gt 1 then
			do;
				call execute('proc print data=allfiles;run;');
			end;
		else
			do;
				call execute("data no_data;msg='No Data';run;");
				call execute('proc print data=no_data;run;');
			end;
	run;

The list_files macro is designed to recursively list all filenames in a specified directory (and its subdirectories) and store them in a dataset. The macro takes two parameters: the directory path (dir) and an optional file extension (ext). If the file extension is provided, the macro will only list files with that extension. Once all files are listed, the macro checks if there are any files in the dataset. If there are, it prints the dataset; otherwise, it prints a message indicating there’s no data.

	%list_files(/home/subhroster20070/sas_inputs);
	%list_files(/home/subhroster20070/sas_inputs,csv);

Here is the complete mind map of the code.

List all files recursively

I tested the code on Linux, and it works. Let me know in the comments if it works on Windows.

Reference:

45805 – List all files within a directory including sub-directories (sas.com)

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.