SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

read multiple txt files automatically

Accepted Solution Solved
Reply
Contributor
Posts: 52
Accepted Solution

read multiple txt files automatically

I have a data file as attached. The below is the code to revise the data.

 

data want;

infile 'C:\Users\AAB006.txt' lrecl=30000;

input;

x=countw(_infile_);

do i=1 to x;

Num=scan(_infile_, i, " ");

output;

end;

drop i x;

run;

 

My next challenge is that: There are thousands of such files in a folder.  Is there way that the SAS code can automatically get the files' names, and process each of them automatically, rather than manually do it one by one.  Appreciate your help.


Accepted Solutions
Solution
‎01-08-2016 02:22 PM
Super User
Super User
Posts: 6,499

Re: read multiple txt files automatically

Use the FILENAME option on the INFILE statement.

Here is an example that will keep the name and also number the files, the rows and column location within the row.

 

data want ;
  length filename fn $200 ;
  infile '*.txt' filename=fn eov=eov length=ll column=cc truncover ;
  fileno+1;
  do row=1 by 1 until (eov) ;
    do col=1 by 1 until (cc>ll);
       input num @ ;
       filename=fn ;
       output;
    end;
    input;
   end;
run;

View solution in original post


All Replies
Super User
Posts: 5,256

Re: read multiple txt files automatically

One alternative is to use an aggregate file location for you filename.
Data never sleeps
Super User
Posts: 17,818

Re: read multiple txt files automatically

Yes, search on here. The method is typically:

 

  1. Get list of file names into data set. This can be done via OS commands typically, ie dir on windows and ls on Unix.
  2. Use the filevar option on the infile statement to specify that the input data set will provide variable names.

If you need one output file per text file you can use the method here.

https://communities.sas.com/t5/General-SAS-Programming/Importing-several-files-into-SAS/m-p/242170#M...

 

There are other solutions listed on the forum as well.

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

 

Solution
‎01-08-2016 02:22 PM
Super User
Super User
Posts: 6,499

Re: read multiple txt files automatically

Use the FILENAME option on the INFILE statement.

Here is an example that will keep the name and also number the files, the rows and column location within the row.

 

data want ;
  length filename fn $200 ;
  infile '*.txt' filename=fn eov=eov length=ll column=cc truncover ;
  fileno+1;
  do row=1 by 1 until (eov) ;
    do col=1 by 1 until (cc>ll);
       input num @ ;
       filename=fn ;
       output;
    end;
    input;
   end;
run;
Contributor
Posts: 52

Re: read multiple txt files automatically

I really love Tom's answer. So simple, but perfectly match my needs.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 395 views
  • 1 like
  • 4 in conversation