DATA Step, Macro, Functions and more

How can I read multiple files placed in a location to one file

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

How can I read multiple files placed in a location to one file

Hi,

 

I am trying to load the data from multiple text files palced in a folder having the same structure to one file. All the text files are having the same headers.

Sample data in one of the file:

Product                       Customer    Qtr 1     Qtr 2         Qtr 3         Qtr 4
Alice_Mutton               ANTON     28080     70200      110400      55125
Aniseed_Syrup            BERGS     31200     78000      60000       14700
Boston_Crab               BOLID       28080     6000        77280      117000
Escargots_Bourgogne BOTTM    117000    20000     16560       122360
Filo_Mix                       ERNSH     112320   18000      92000       260715
Geitost                         GODOS    6240       28080      24840       41952

 

Also the names of the file are cust101, cust102, cust103.........Cust200...

 

Can anyone please suggest an efficient way to read the data from all these text file in to a single file.

 

Regards,

Saket

 

 


Accepted Solutions
Solution
‎04-23-2016 02:26 PM
Super User
Posts: 17,868

Re: How can I read multiple files placed in a location to one file

Here's my recommended solution:

 

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

 

1. You need your input/informat/format statement for reading a single file.

2. In the code in link above, change the path, and use a wildcard to reference the data.

3. Make sure to use the EOV= variable to avoid reading the header in each file.

 

View solution in original post


All Replies
SAS Super FREQ
Posts: 8,743

Re: How can I read multiple files placed in a location to one file

Hi:
This Tech Support note describes one method:
https://support.sas.com/techsup/technote/ts581.pdf

cynthia
Contributor
Posts: 39

Re: How can I read multiple files placed in a location to one file

You can read multiple files using wildcrds. The code below is not tested but should help get you started.

 

%let data_filepath = c:\...\cust*;  * modify path as appropriate for your system;

 

data temp;
infile firstobs=2 "&data_filepath";
input product $ customer $ qtr1-qtr4;
run;

Solution
‎04-23-2016 02:26 PM
Super User
Posts: 17,868

Re: How can I read multiple files placed in a location to one file

Here's my recommended solution:

 

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

 

1. You need your input/informat/format statement for reading a single file.

2. In the code in link above, change the path, and use a wildcard to reference the data.

3. Make sure to use the EOV= variable to avoid reading the header in each file.

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 241 views
  • 0 likes
  • 4 in conversation