Published on :
Macro EXTERNAL

Macro dirfp2sas: Listing Windows files to SAS table

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
This macro uses a system command via a 'filename pipe' to execute a shell command (Windows-specific 'dir'/'for') to list files matching the provided pattern. The result is stored in a SAS© table containing the full path and the lowercase filename. Note: This code contains Windows-specific commands and will require adaptation to work on a standard SAS© Viya (Linux) environment.
Data Analysis

Type : EXTERNAL


Data originates from a system command (Pipe) listing files in the specified directory.

1 Code Block
MACRO
Explanation :
Macro definition, log options management, and 'filename pipe' configuration to execute the Windows file listing system command.
Copied!
1%macro dirfp2sas(filepattern,dsout);
2 
3 %local savopts;
4 %let savopts=%sysfunc(getoption(NOTES));
5 
6 %IF not %LENGTH(&dsout) %THEN %let dsout=_dirfp2sas;
7 
8 options nonotes;
9 
10 filename _dirfp pipe
11 "echo off & for %nrstr(%f) in (""&filepattern"") do echo %nrstr(%f)";
2 Code Block
DATA STEP Data
Explanation :
Reading the system command's output stream and creating the output table with the full path and extracted filename.
Copied!
1 DATA &dsout;
2 LENGTH filename $ 300 lcfname $ 200;
3 INFILE _dirfp;
4 INPUT;
5 filename=trim(_infile_);
6 lcfname=lowcase(scan(filename,-1,"\"));
7 RUN;
3 Code Block
CLEANUP
Explanation :
Filename cleanup and restoration of initial SAS options.
Copied!
1 filename _dirfp CLEAR;
2 
3 options &savopts;
4 
5%mend dirfp2sas;
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 : Public domain software. Roland Rashleigh-Berry.