Help using Base SAS procedures

Importing multiple txt files - using a Macro? Help

Reply
Occasional Contributor
Posts: 15

Importing multiple txt files - using a Macro? Help


Hi,

Due to my lack / limited use of macro's, I'm hoping someone would be kind enough to help me out.

I have a number of files saved in a folder:

Folder:

Beacon_Data\data\sas\

Files:

ERCH_1.txt

ERCH_2.txt

:::::::::::::::::

:::::::::::::::::

ERCH_50.txt

I have used a basic proc input to bring in one file (I can format after) but I've searched the web on how to read all the files in & I fail when I get to reading the what files are in the directory..

PROC IMPORT OUT= WORK.All_ERCH

DATAFILE= "\data\sas\ERCH_1.txt" dbms=dlm    replace; delimiter= ';'

getnames=yes;

run;

Instead of confusing matters with the code I have copied from various sites, I hope this is a easy program to help with.

Many thaanks

Super User
Posts: 19,800

Re: Importing multiple txt files - using a Macro? Help

You don't need a macro, you can use a wildcard to import the files. Import once and get your file structure.

http://listserv.uga.edu/cgi-bin/wa?A2=ind1210a&L=sas-l&H=1&F=&S=&P=1046


Occasional Contributor
Posts: 15

Re: Importing multiple txt files - using a Macro? Help

I have tried using the wildcard, if you mean:

PROC IMPORT OUT= WORK.All_ERCH

DATAFILE= "\beacon_data\sas\*.txt" dbms=dlm    replace; delimiter= ';'

getnames=yes;

run;

This does import all the tables, it includes all the headers from each table.  I can clean the data, but I'd like a more efficient way to do the import.

Super User
Posts: 10,028

Re: Importing multiple txt files - using a Macro? Help

How about this. Or you can use it to write a macro. they are the same.

data have;
 input file : $20.;
cards;
ERCH_1.txt
ERCH_2.txt
;
run;
data _null_;
 set have;
 call execute('proc import datafile="c:\temp\'||strip(file)||'" out='||strip(scan(file,1,'.'))||' dbms=dlm replace; delimiter=";" ;
getnames=yes;run;');
run;


Ksharp

Super User
Posts: 19,800

Re: Importing multiple txt files - using a Macro? Help

24707 - Reading multiple files with PROC IMPORT

Click on the full code tab to get the code.

There is also a second option (linked from the first page) that shows how to import it all into one dataset.

Good luck.

Occasional Contributor
Posts: 9

Re: Importing multiple txt files - using a Macro? Help

Here is a link to read multiple raw data files with the same structure in one data step. Hope this helps.

http://www.ats.ucla.edu/stat/sas/faq/multi_file_read.htm

Thanks,

Ask a Question
Discussion stats
  • 5 replies
  • 4894 views
  • 0 likes
  • 4 in conversation