O'Reilly logo

SAS 9.4 Macro Language, 5th Edition by

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

run;
Use the %END statements to close the %DO blocks.
%end;
%end;
%end;
%end;
If the directory cannot be opened, the %ELSE is executed to write a note to the log:
%else %put &dir cannot be open.;
Use the DCLOSE function to close the directory.
%let rc=%sysfunc(dclose(&did));
Invoke the macro. The first parameter is the directory where the files exist. The second
parameter is the extension of the files that you are importing.
%drive(c:\temp,csv)
Example 2: List All Files within a Directory
Including Subdirectories
Details
This macro lists all files within the directory that is passed to the macro including any
subdirectories. This macro currently works for Windows platforms, but could be
modified for other operating environments.
Note: This example will not run if you use the LOCKDOWN system option.
Program
%macro list_files(dir,ext);
%local filrf rc did memcnt name i;
%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;
%return;
%end;
%do i = 1 %to %sysfunc(dnum(&did));
%let name=%qsysfunc(dread(&did,&i));
%if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
%put &dir\&name;
%end;
%else %if %qscan(&name,2,.) = %then %do;
%list_files(&dir\&name,&ext)
%end;
Example 2: List All Files within a Directory Including Subdirectories 449
%end;
%let rc=%sysfunc(dclose(&did));
%let rc=%sysfunc(filename(filrf));
%mend list_files;
%list_files(c:\temp,sas)
Program Description
Begin the macro definition with two parameters.
%macro list_files(dir,ext);
%local filrf rc did memcnt name i;
Create two macro variables, &RC will contain the results from the FILENAME
function. The value is 0 if it is successful or not a 0 if it is not successful. The
FILENAME function assigns the filrf (a system-generated fileref) to the directory that is
passed to the macro (&DIR). &DID contains the results from the DOPEN function that
opens the directory. DOPEN returns a directory identifier value of
0 if the directory
cannot be opened.
%let rc=%sysfunc(filename(filrf,&dir));
%let did=%sysfunc(dopen(&filrf));
Use the %IF statement to make sure the directory can be opened. If not, end the macro.
%if &did eq 0 %then %do;
%put Directory &dir cannot be open or does not exist;
%return;
%end;
Use the %DO statement to loop through the entire directory based on the number of
members returned from the DNUM function.
%do i = 1 %to %sysfunc(dnum(&did));
Create a macro variable named &NAME that will contain each filename in the directory
that is passed to the macro. The DREAD function is used to retrieve the name of each
file.
%let name=%qsysfunc(dread(&did,&i));
Use the %IF statement to see whether the extension matches the second parameter value
that is supplied to the macro. If the condition is true, then print the full name to the log.
%QSCAN is used to pull off the extension of the filename (&NAME) by using -1 as the
second argument. %QUPCASE is used on both sides of the equal to ensure the case
matches.
%if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
%put &dir\&name;
%end;
If the previous %IF condition is false, then use %ELSE %IF statements to see whether
the name contains an extension. If no extension is found, then assume that the name is a
directory and call the macro again. This ensures that all subdirectories are read.
%else %if %qscan(&name,2,.) = %then %do;
%list_files(&dir\&name,&ext)
%end;
Use the %END statement to close the %DO block.
450 Appendix 5 SAS Macro Examples

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required