Published on :
Macro EXTERNAL

Dynamic Recursive Execution of SAS Files

This code is also available in: Deutsch Español Français
Attention : This code requires administrator privileges.
The code defines a '%findsas' macro that uses a system command (PIPE) to list files with the .sas© extension in a given directory. It then uses 'CALL EXECUTE' within a DATA _NULL_ step to dynamically generate '%include' statements for each found file. Note that the 'dir /b /s' command is Windows-specific and will need to be adapted for a Linux environment (standard SAS© Viya).
Data Analysis

Type : EXTERNAL


File list obtained via a system command (OS).

1 Code Block
DATA STEP
Explanation :
Macro definition. Uses 'filename pipe' to execute the DOS 'dir' command and retrieve the list of files. The DATA step iterates through this list and executes each file via '%include'.
Copied!
1%macro findsas(dir);
2
3 filename filelist pipe "dir /b /s &dir\*.sas";
4 
5 DATA _null_;
6 INFILE filelist truncover;
7 INPUT filename $100.;
8 call execute(cats('%include ', quote(trim(filename)), ';'));
9 RUN;
10 
11%mend findsas;
2 Code Block
MACRO CALL
Explanation :
Macro call with a path constructed via a macro variable '&path'. A commented line shows an example of an absolute Windows path.
Copied!
1%findsas(&path\mysaspgms)
2%*findsas(S:\workshop\mysaspgms)
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.