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
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?
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.
I dont need it to be appended. I just need to append
To append or not to append, that seems to be the question ...
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.