DATA Step, Macro, Functions and more

Reading a Directory Correctly in a Filename Statement

Reply
Contributor
Posts: 33

Reading a Directory Correctly in a Filename Statement

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

 

Frequent Contributor
Posts: 102

Re: Reading a Directory Correctly in a Filename Statement

Is it possible that SAS is reading forward slash in your macro variable &wheredeyat\ as part of the macro? Have you tried using "&wheredeyat.\ with the period as macro string terminator?
Trusted Advisor
Posts: 1,369

Re: Reading a Directory Correctly in a Filename Statement

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 */

 

Valued Guide
Posts: 505

Re: Reading a Directory Correctly in a Filename Statement

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

Ask a Question
Discussion stats
  • 3 replies
  • 203 views
  • 0 likes
  • 4 in conversation