Sample 24707: Reading multiple files with PROC IMPORT
/* Specify the path that you want to read ALL files from, so to test, */
/* create test data in a folder all by itself. */
/* */
/* This sample illustrates Windows syntax. To run this sample on OS/2, */
/* UNIX, or OpenVMS, be sure to use your OS syntax on the FILE statements, */
/* FILENAME statement and the DATAFILE= in the PROC IMPORT. The result */
/* of the FILENAME statement may need to be parsed differently as well */
/* so that &FNAMEn resolves to the file's name and &PEXTn resolves to the */
/* file's name and extension. */
/* */
/* See the Downloads tab to access a modified version of this sample */
/* that can be run on Unix. */
/* Create external file EXTFILE1 */
data _null_;
/* Edit the path for your OS */
file 'c:\junk\extfile1.txt';
put "05JAN2001 6 W12301 1.59 9.54";
put "12JAN2001 3 P01219 2.99 8.97";
run;
/* Create external file EXTFILE2 */
data _null_;
/* Edit the path for your OS */
file 'c:\junk\extfile2.txt';
put "02FEB2001 1 P01219 2.99 2.99";
put "05FEB2001 3 A00901 1.99 5.97";
put "07FEB2001 2 C21135 3.00 6.00";
run;
/* Create the external file EXTFILE3 */
data _null_;
/* Edit the path for your OS */
file 'c:\junk\extfile3.txt';
put "06MAR2001 4 A00101 3.59 14.36";
put "12MAR2001 2 P01219 2.99 5.98";
run;
/* Read all filenames and put them into macro variables. The &FNAME variables store */
/* the names of the files only and &PEXT variables store the names of the files and */
/* the extensions. */
/* */
/* Note: For releases SAS 9.0 or above, CALL SYMPUTX can be used to save two */
/* function calls while creating the macro variables. For example, */
/* */
/* call symputx('fname'||put(i,8.-L),scan(trim(fname),1,'.')); */
/* Edit the pipe information for your OS */
filename blah pipe 'dir C:\Junk /b';
data _null_;
infile blah truncover end=last;
/* Edit length as needed */
length fname $20;
input fname;
i+1;
/* The parsing of FNAME may need to be changed depending on your OS. */
call symput('fname'||trim(left(put(i,8.))),scan(trim(fname),1,'.'));
call symput('pext'||trim(left(put(i,8.))),trim(fname));
if last then call symput('total',trim(left(put(i,8.))));
run;
/* Within a macro, run the PROC IMPORT code so that each filename is placed into */
/* the code appropriately. The PROC PRINT in the macro is just to show the */
/* results for testing purposes, and can be removed later. */
%macro test;
%do i=1 %to &total;
proc import datafile="c:\Junk\&&pext&i"
out=work.&&fname&i
dbms=dlm replace;
delimiter=' ';
getnames=no ;
run;
proc print data=work.&&fname&i;;
title &&fname&i;
run;
%end;
%mend;
/* Invoke the macro */
%test
extfile1
Obs VAR1 VAR2 VAR3 VAR4 VAR5
1 05JAN2001 6 W12301 1.59 9.54
2 12JAN2001 3 P01219 2.99 8.97
extfile2
Obs VAR1 VAR2 VAR3 VAR4 VAR5
1 02FEB2001 1 P01219 2.99 2.99
2 05FEB2001 3 A00901 1.99 5.97
3 07FEB2001 2 C21135 3 6
extfile3
Obs VAR1 VAR2 VAR3 VAR4 VAR5
1 06MAR2001 4 A00101 3.59 14.36
2 12MAR2001 2 P01219 2.99 5.98
Click
here
to access a version of Sample 157 that uses Unix syntax.
Read multiple files using PROC IMPORT without having to run the code manually for each file.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Procedures ==> IMPORT Common Programming Tasks ==> Reading and Writing External Data
|
| Date Modified: | 2006-11-21 03:02:44 |
| Date Created: | 2004-09-30 14:09:06 |
Operating System and Release Information
| SAS System | Base SAS | Tru64 UNIX | 8 TS M0 | n/a |
| Solaris | 8 TS M0 | n/a |
| OpenVMS Alpha | 8 TS M0 | n/a |
| HP-UX IPF | 8 TS M0 | n/a |
| Linux | 8 TS M0 | n/a |
| HP-UX | 8 TS M0 | n/a |
| ABI+ for Intel Architecture | 8 TS M0 | n/a |
| AIX | 8 TS M0 | n/a |
| 64-bit Enabled Solaris | 8 TS M0 | n/a |
| 64-bit Enabled AIX | 8 TS M0 | n/a |
| 64-bit Enabled HP-UX | 8 TS M0 | n/a |
| OS/2 | 8 TS M0 | n/a |