Begin the macro definition with two parameters.
%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.
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;
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
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
%if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
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;
Use the %END statement to close the %DO block.
450 Appendix 5 • SAS Macro Examples