DATA Step, Macro, Functions and more

Import multiple txt files into sas

Reply
Frequent Contributor
Posts: 126

Import multiple txt files into sas

Hi all,

Quite new to the macro world so here it goes,

I have a significant amount of txt files that i wish to upload into sas and then merge them . example of name of the files is as following:

zomb_200911.txt

zomb_200912.txt

zomb_201001.txt

zomb_201002.txt

zomb_201003.txt and etc.

They all contain the same info, so i was wondering is there any simple macro i could apply for this situation?

Thnx in advance

PROC Star
Posts: 7,474

Import multiple txt files into sas

Posted in reply to chemicalab

If by "merge" you mean concatenate (i.e., create one file that includes the records from all of the other files) then you may only need something as simple as:

data want;

  infile '*.txt';

  input yourvariable_list ;

run;

Frequent Contributor
Posts: 126

Import multiple txt files into sas

Yes concatenate it is sorry, one last question, what if i not sure how many variables are contained in each txt file and i have no headers, how could i face that?

PROC Star
Posts: 7,474

Import multiple txt files into sas

Posted in reply to chemicalab

SAS has no way of guessing what your files contain.  For that, you either have to be able to recognize what the data represent or ask the person who created the file(s).  You could always bring one of the files up in a text editor (like notepad or wordpad) and see if you can recognize the key parameters, e.g., how many fields, how are they separated, are character fields enclosed in quotes, what each field represents, etc.).

Then, you can use proc import to import one of the files and, after it has done so, press F3 to get SAS to spit back the actual code it wrote to import the file.

From there, you should be well on your way to answering your own question.

Frequent Contributor
Posts: 126

Import multiple txt files into sas

thank for clearing that up, i have tried the following

since i found some dataset with var names

data sbpno.want;

  infile 'datapath\*.txt';

  input AcNum          Sum          D ;

run;

but it doesnt seem to capture the data containted in the files , any last guess or maybe i miss understood something from the above mentioned

PROC Star
Posts: 7,474

Import multiple txt files into sas

Posted in reply to chemicalab

It would help if you could post one of the files or at least the first 10 records or so, as well as the messages you received in your log.  Like I mentioned, I would try to import one of the files using proc import and then seeing what code it wrote.  Chances are that you will have to include some informat and format lines so that SAS knows what kind of data it is reading.  But, even then, you would have to tell SAS what kind of file it is, i.e., delimited (and if so with what kind of delimiter).

You will also probably have to input the files in two groups, one that has a header record and one that doesn't, and specify on which row the data actually start in each set.

Super User
Posts: 10,028

Import multiple txt files into sas

Posted in reply to chemicalab

Just as Art said. For your situation , the best way is to use proc import, let SAS do it for you automatically.

But I recommend to use filename + pipe to get filename firstly ,then use macro to loop proc import .

Ksharp

Frequent Contributor
Posts: 126

Import multiple txt files into sas

Posted in reply to chemicalab

Thank you both for your answers , i will use the proc import to loop around

thnx again

PROC Star
Posts: 7,474

Import multiple txt files into sas

Posted in reply to chemicalab

You never did post a couple of records from each of your two types of files (i.e., those that do and those that do not have header records).  It is quite possible to write code that can determine whether or not a header record exists and do all of the imports in one pass.

Ask a Question
Discussion stats
  • 8 replies
  • 1099 views
  • 0 likes
  • 3 in conversation