BookmarkSubscribeRSS Feed
chemicalab
Fluorite | Level 6

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

8 REPLIES 8
art297
Opal | Level 21

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;

chemicalab
Fluorite | Level 6

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?

art297
Opal | Level 21

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.

chemicalab
Fluorite | Level 6

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

art297
Opal | Level 21

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.

Ksharp
Super User

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

chemicalab
Fluorite | Level 6

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

thnx again

art297
Opal | Level 21

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 2262 views
  • 0 likes
  • 3 in conversation