BookmarkSubscribeRSS Feed
chandan_mishra
Obsidian | Level 7

Hello

 

I want to import multiple text files with .txt extension. The sample looks like this:

 

File1.txt

One Two Three Four Five

1 2 3 4 5

 

File2.txt

One Two Three Four Five Six Seven

1 2 3 4 5 6 7

 

File3.txt

One Mode Two File Three Extension Four Five Six Seven

1 SAS 2 F 3 txt 4 5 6 7

 

So, all these three files have separate headers. Some of the headers are common but some are different. 

 

I want to import all these files with headers. I dont need it to be appended. I just need to append these three datasets with the headers which are not common. Also, in this case I dont want to specify the header names as input everytime. In the real file, I have almost 150 variables which makes it difficult to specify the variable names under input.

 

Thanks

Chandan mishra

6 REPLIES 6
Shmuel
Garnet | Level 18

Is the header, i.e. the variable names, part of the text file and is it always the first row?

Is the delimiter between the variables (either the header or the values) is a space or other character?

chandan_mishra
Obsidian | Level 7
Yes the first row is the header always and the txt file is tab delimited.
Kurt_Bremser
Super User

So you have a big pile of data that does not fit together, but want to fit it together in a simple way? Forget it.

 

In order to append at least the common variables into a final dataset, you have to make sure that the attributes of those variables fit. That is only possible by writing each data step yourself, proc import won't do.

chandan_mishra
Obsidian | Level 7
Hello

I don’t need to append the datasets. Just need to import all of them as separate datasets.

Thanks
Chandan mishra
Kurt_Bremser
Super User

Run a proc import on one of your files:

proc import
  datafile="your_complete_path"
  out=datasetname
  dbms=tab
  replace
;
run;

and see if it works as desired.

Then, check if your filenames can be used as valid SAS dataset names. If yes, wrap the above code into a macro definition:

%macro import_one(fname);
proc import
  datafile="your_path/&fname..txt"
  out=yourlib.&fname.
  dbms=tab
  replace
;
run;
%mend;

which you can then call from a dataset that contains your filenames. If you need help creating such a dataset automatically from the directory, ask.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 884 views
  • 0 likes
  • 3 in conversation