BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
yashpande
Obsidian | Level 7

I have got several txt files on unix platform. Names are different but structure is same. I need to create only one dataset of all these files and that should have data from all.

My below one datastep works fine but i am unable to somehow get all files together in one dataset.  and I also want one column which says the name of the text file from which this data is coming

Data new;

infile "/gpfs1/ana_layer/file1.txt' ;

input @1 id $3 @5 age;

run;

this works correctly for one file but i am unable to generalise it for the entire directory and also unable to get the filename as one more column in the resultant dataset

Thanks a lot in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Try this:

%let path=where_your_file_are_stored;

%let filnam=common_part_of_filename;

filename oscmd pipe "ls &path./&filnam.*";

data want;

infile oscmd;

length filnam myfilename filnamstore $ 300;

input filnam;

infile in filevar=filnam filename=myfilename end=done;

do while (not done);

  *read external file as usual;

  filnamstore=myfilename;

  output;

end;

run;

Built upon the example in Statements: INFILE Statement - 9.2

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

There are at least 30 separate topics using a variety of methods on these forums.  Just search for "read multiple files from directory".

Example:

BrunoMueller
SAS Super FREQ

Hi

the INFILE statement supports wildcards, the FILENAME= option names a variable that will contain the name of the file that a records was read from. Please note, that the variable named by the FILENAME= option is dropped automatically, so you need to assign the value to some other variable. See also example 5 in the doc for more ideas on the list of files to read SAS(R) 9.4 Statements: Reference, Third Edition

data someData;
  length _xFilename $ 256;
 
infile "c:\temp\*.csv" filename=_xfilename;
  xFilename = _xFilename;
 
input
    @
1 line $10.
  ;
run;

bruno
rajneelram87
Calcite | Level 5

Thank you @BrunoMueller , i tried your code, made very small changes and it works! I was able to pull in several flat files as i was hoping for. 

Kurt_Bremser
Super User

Try this:

%let path=where_your_file_are_stored;

%let filnam=common_part_of_filename;

filename oscmd pipe "ls &path./&filnam.*";

data want;

infile oscmd;

length filnam myfilename filnamstore $ 300;

input filnam;

infile in filevar=filnam filename=myfilename end=done;

do while (not done);

  *read external file as usual;

  filnamstore=myfilename;

  output;

end;

run;

Built upon the example in Statements: INFILE Statement - 9.2

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 7136 views
  • 4 likes
  • 5 in conversation