Hello,
I am having a difficult time getting SAS to read a directory correctly. I am creating a process that notifies me of any errors in my log files from nightly processes. At this point I am merely trying to get the list of all log files located in the specific folder, but SAS does not seem to read my path correctly. Here is my code:
*Logs location;
%LET wheredeyat=\\vs-app-SAS01\Scheduled Tasks\Task Logs;
*List all the files in the current directory;
FILENAME dirlist PIPE 'dir /B "&wheredeyat\*.log"'; /* dir /B */
DATA dirlist;
LENGTH fname $256;
INFILE dirlist LENGTH=reclen TRUNCOVER;
INPUT fname $varying256. reclen;
RUN;
Here is the log to the above code:
NOTE: Writing TAGSETS.SASREPORT13(EGSR) Body file: EGSR
21
22 GOPTIONS ACCESSIBLE;
23 OPTIONS MSGLEVEL=I MPRINT=ON SORTSIZE=512m;
24
25
26 *Logs location;
27 %LET wheredeyat=\\vs-app-SAS01\Scheduled Tasks\Task Logs;
28
29 *List all the files in the current directory;
30 FILENAME dirlist PIPE 'dir /B "&wheredeyat\*.log"'; /* dir /B */
31 DATA dirlist;
32 LENGTH fname $256;
33 INFILE dirlist LENGTH=reclen TRUNCOVER;
34 INPUT fname $varying256. reclen;
35 RUN;
NOTE: The infile DIRLIST is:
Unnamed Pipe Access Device,
PROCESS=dir /B "&wheredeyat\*.log",RECFM=V,
LRECL=32767
Stderr output:
The system cannot find the file specified.
NOTE: 0 records were read from the infile DIRLIST.
NOTE: The data set WORK.DIRLIST has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.04 seconds
cpu time 0.01 seconds
Your filename statement may cause an issue:
FILENAME dirlist PIPE 'dir /B "&wheredeyat\*.log"'; /* dir /B */
macro variable will not be resolves inside single quotes ('..&xxxx ....').
try use only double-quotes:
FILENAME dirlist PIPE "dir /B ""&wheredeyat\*.log"" "; /* dir /B */
Reading a Directory Correctly in a Filename Statement
If you double the double quotes instead of using single quotes it should work. At least it worked on my power workstation.
see working code
HAVE THIS TREE STRUCTURE
========================
Folder PATH listing for volume backup
Volume serial number is 5EA1-ABB0
D:\TXT
\---wheredeyat
foo1st.log
foo1st.txt
foo2nd.log
foo2nd.txt
WANT
====
FNAME=foo1st.log
FNAME=foo2nd.log
WORKING CODE ( note the dubling of quotes)
==========================================
FILENAME dirlist PIPE "dir /B ""&wheredeyat\*.log""";
FULL SOLUTION
=============
* create a directory and some file;
data _null_;
newdir=dcreate('wheredeyat',"d:/txt/");
rc=dosubl(resolve('
data _null_;
file "d:/txt/wheredeyat/foo1st.log";
put "foo1st";
file "d:/txt/wheredeyat/foo2nd.log";
put "foo2nd";
'));
stop;
run;quit;
* layout the tree;
filename pipetree pipe "tree ""d:/txt"" /F /A" lrecl=5000;
data _null_;
infile pipetree truncover;
input dirlist $char1000.;
*if index(dirlist,'.') =0;
put dirlist;
run;
SOLUTION WITH MINOR MODIFICATION OF YOUR CODE
%LET wheredeyat=d:\txt\wheredeyat;
*List all the files in the current directory;
FILENAME dirlist PIPE "dir /B ""&wheredeyat\*.log""";
DATA dirlist;
LENGTH fname $256;
INFILE dirlist LENGTH=reclen TRUNCOVER;
INPUT fname $varying256. reclen;
put fname=;
RUN;
FNAME=foo1st.log
FNAME=foo2nd.log
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.