Data comes from an external source: the list of files from an operating system directory, obtained via the 'dir' command (Windows) executed by a 'filename pipe'. This information is then processed to create an internal SAS dataset or displayed in the log.
1 Code Block
MACRO DEFINITION & INITIALIZATION
Explanation : This block defines the `dirlist` macro with its `path` and `outdn` parameters. It also initializes default values for these parameters if they are not provided. `%superq` is used to check if the parameters are empty, without attempting to resolve them, which avoids errors for uninitialized values.
Copied!
%macro dirlist(path, outdn);
/*set the defualt value of the required params if the value is null*/
%if %superq(path)= %then %let path=&pdir.data;
%if %superq(outdn)= %then %let outdn=_null_;
1
%macro dirlist(path, outdn);
2
/*set the defualt value of the required params if the value is null*/
3
%IF %superq(path)= %THEN %let path=&pdir.DATA;
4
%IF %superq(outdn)= %THEN %let outdn=_null_;
2 Code Block
FILENAME PIPE
Explanation : This `filename pipe` statement is crucial. It creates a fileref named `filelist` which, instead of pointing to a physical file, executes an operating system command (`dir "&path" /o:n /b`) and processes its output as an input file. `dir /o:n /b` lists files from the directory specified by `&path` (ordered by name, raw format). It's important to note that 'dir' is a Windows command; for a Linux environment (common for SAS Viya), a command like 'ls' would be needed.
Explanation : This block is a DATA step that reads the output of the 'dir' command via the `filelist` fileref. Each line of the output (corresponding to a file name) is read into the `fullname` variable. If the `outdn` parameter is `_null_`, the `fullname` is directly written to the SAS log. Otherwise, the code parses the `fullname` to extract the extension (`extname`) and the file name without extension (`filename`). The `index`, `scan`, `substr`, `length`, `lowcase`, and `call missing` functions are used for this parsing.
Copied!
data &outdn;
/*The max length of the name of a PC file is 255*/
length fullname $255 %if &outdn ne _null_ %then filename $255 extname $10 ; ;
infile filelist truncover dlm="|";
input fullname;
%if &outdn=_null_ %then put fullname %str(;) ;
%else %do;
if index(fullname, ".") then do;
extname=lowcase(scan(fullname, -1, '.'));
filename=substr(fullname, 1, length(fullname)-length(extname)-1);
end;
else do;
filename=fullname;
call missing(extname);
end;
%end;
run;
1
DATA &outdn;
2
/*The max length of the name of a PC file is 255*/
Explanation : If an output dataset (`outdn`) has been specified (i.e., not `_null_`), this block executes a `PROC SORT`. This procedure sorts the dataset created by the DATA step according to the `extname` variable (file extension), thus organizing files by type.
Copied!
%if &outdn ne _null_ %then %do;
proc sort data=&outdn;
by extname;
run;
1
%IF &outdn ne _null_ %THEN %DO;
2
PROC SORTDATA=&outdn;
3
BY extname;
4
RUN;
5 Code Block
PROC PRINT
Explanation : Following `PROC SORT` (if `outdn` is not `_null_`), this block uses `PROC PRINT` to display the contents of the `outdn` dataset in the SAS output. A descriptive title is defined for the procedure output, then cleared after execution. This allows viewing the listed files and their details (full name, file name, extension) directly in the SAS output.
Copied!
title "The &path contents";
proc print data=&outdn;
run;
title;
%end;
1
title "The &path contents";
2
PROC PRINTDATA=&outdn;
3
RUN;
4
title;
5
%END;
6 Code Block
CLEANUP & MACRO END
Explanation : This final block is responsible for cleanup and macro closure. The `filename filelist clear;` statement frees the `filelist` fileref, which is good practice to avoid conflicts or accidental subsequent use. Finally, `%mend dirlist;` marks the end of the `dirlist` macro definition.
Copied!
filename filelist clear;
%mend dirlist;
1
filename filelist clear;
2
%mend dirlist;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Name : dirlist.sas
Author: Jun Fang June, 2016
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.