Sample 24710: How to read all the log files in a specified subdirectory and create a data set with their error messages
Use the PIPE keyword in the FILENAME statement to send the "LS" or "DIR" command to the operating system to generate a list of the SAS log files. Use the INFILE statement with the FILEVAR= option to read them in.
Note:
In this code sample there are two %LET and two FILENAME
statements. Use the appropriate %LET and FILENAME statement
pair for your operating system. Change the value of the
SUBDIR macro variable appropriately.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* If running on unix, use the following %LET and FILENAME statements */
* %let subdir=~/test/;
* filename dir pipe "ls -1 &subdir | grep .log";
/* If running on a PC, use the following %LET and FILENAME statements instead. */
/* Note, /B lists each directory name or file name, one per line, including */
/* the file name extension. See DIR command documentation within Windows HELP */
/* for additional option information. */
%let subdir=c:\1test\;
filename dir pipe "dir &subdir.*.log /B";
data new;
infile dir truncover;
/* Read a log file from the subdirectory*/
input filename $80.;
filename="&subdir" || filename;
length lname logfile $30;
infile dummy filevar=filename filename=lname end=done truncover;
do while (not done);
/* Read a record from the log file*/
input test $5. @;
if test='ERROR' then do;
input @6 msg $76.;
logfile=lname;
output;
end;
else input;
end;
run;
/* Results will depend on the contents of the log files */
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
l
o
g
f t
O i e m
b l s s
s e t g
1 c:\1test\first.log ERROR 22-322: Syntax error, expecting one of the following: ;, CANCEL, PGM.
2 c:\1test\first.log ERROR 76-322: Syntax error, statement will be ignored.
3 c:\1test\second.log ERROR 22-322: Syntax error, expecting one of the following: ;, CANCEL, PGM.
4 c:\1test\second.log ERROR 76-322: Syntax error, statement will be ignored
Use PIPE in the FILENAME statement to access the SAS log
files and use the INFILE statement with the FILEVAR= option
to read them in.
This sample is specific to SAS logs, but can be adapted to any type file.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step Common Programming Tasks ==> Utilities
|
Date Modified: | 2019-06-11 15:00:29 |
Date Created: | 2004-09-30 14:09:06 |
Operating System and Release Information
SAS System | Base SAS | Tru64 UNIX | n/a | n/a |
Solaris | n/a | n/a |
OpenVMS Alpha | n/a | n/a |
HP-UX IPF | n/a | n/a |
Linux | n/a | n/a |
HP-UX | n/a | n/a |
AIX | n/a | n/a |
ABI+ for Intel Architecture | n/a | n/a |
64-bit Enabled Solaris | n/a | n/a |
64-bit Enabled HP-UX | n/a | n/a |
64-bit Enabled AIX | n/a | n/a |
Windows | n/a | n/a |
Microsoft® Windows® for 64-Bit Itanium-based Systems | n/a | n/a |
OS/2 | n/a | n/a |