Sample 24712: Read multiple external files with the same layout into a single SAS data set
Read multiple external files with the same layout into a
single SAS data set using the FILEVAR= option of the INFILE
statement. Also, see sample
24714, Reading multiple files using file concatenation.
Data about the files to be read can be passed to FILEVAR=
via DATALINES (as illustrated in this sample), via FILENAME
pipe, or via a file containing the fully qualified paths. See
TS-581 "Using FILEVAR= to read multiple external files in a DATA Step"
for more
information.
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.
/* Create external file EXTFILE1 */
data _null_;
file 'c:\temp\extfile1.txt';
put "05JAN2001 6 W12301 1.59 9.54";
put "12JAN2001 3 P01219 2.99 8.97";
put "16JAN2001 1 A00101 3.00 3.00";
put "19JAN2001 3 A00101 3.00 9.00";
put "24JAN2001 2 B90035 2.59 5.18";
run;
/* Create external file EXTFILE2 */
data _null_;
file 'c:\temp\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";
put "14FEB2001 7 B90035 2.59 18.13";
put "20FEB2001 6 A00901 1.99 11.94";
put "27FEB2001 1 W12301 1.59 1.59";
put "27FEB2001 2 C00300 1.00 2.00";
put "28FEB2001 2 B90035 2.59 5.18";
run;
/* Create the external file EXTFILE3 */
data _null_;
file 'c:\temp\extfile3.txt';
put "06MAR2001 4 A00101 3.59 14.36";
put "12MAR2001 2 P01219 2.99 5.98";
put "13MAR2001 2 A00101 3.00 6.00";
put "16MAR2001 3 B90035 2.59 7.77";
put "16MAR2001 1 W99201 5.50 5.50";
put "21MAR2001 3 C30660 2.00 6.00";
put "29MAR2001 5 A00901 1.99 9.95";
run;
/* Path to files to be read are in the DATALINES. */
/* Each file is read in turn with the same INPUT statement. */
/* The END= variable is set to 1 each time the DATA step */
/* comes to the end of a file. */
/* */
/* Read the name of the file to be read from the DATALINES and */
/* store it in FIL2READ. The file is then read in the DO WHILE */
/* loop. At the end of the file, the DO loop ends, control */
/* passes back to the top of DATA step and the process starts */
/* over again until all files have been read. */
/* */
/* The argument "dummy" in the INFILE statement is a place- */
/* holder used in place of a file reference. */
data one;
infile datalines;
/* Ensure fully qualified path will fit in FIL2READ */
length fil2read $40;
/* Input path of file to be read from DATALINES */
input fil2read $;
infile dummy filevar=fil2read end=done;
do while(not done);
/* Input statement for files to be read */
input @1 date date9. @11 quanity item $ price totcost;
output;
end;
datalines;
c:\temp\extfile1.txt
c:\temp\extfile2.txt
c:\temp\extfile3.txt
;
proc print data=one;
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.
Obs date quanity item price totcost
1 14980 6 W12301 1.59 9.54
2 14987 3 P01219 2.99 8.97
3 14991 1 A00101 3.00 3.00
4 14994 3 A00101 3.00 9.00
5 14999 2 B90035 2.59 5.18
6 15008 1 P01219 2.99 2.99
7 15011 3 A00901 1.99 5.97
8 15013 2 C21135 3.00 6.00
9 15020 7 B90035 2.59 18.13
10 15026 6 A00901 1.99 11.94
11 15033 1 W12301 1.59 1.59
12 15033 2 C00300 1.00 2.00
13 15034 2 B90035 2.59 5.18
14 15040 4 A00101 3.59 14.36
15 15046 2 P01219 2.99 5.98
16 15047 2 A00101 3.00 6.00
17 15050 3 B90035 2.59 7.77
18 15050 1 W99201 5.50 5.50
19 15055 3 C30660 2.00 6.00
20 15063 5 A00901 1.99 9.95
Read multiple external files with the same layout into a
single SAS data set using the FILEVAR= option of the INFILE
statement.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step Common Programming Tasks ==> Reading and Writing External Data
|
| Date Modified: | 2005-12-08 11:34:24 |
| Date Created: | 2004-09-30 14:09:07 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |