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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

4 REPLIES 4
LinusH
Tourmaline | Level 20
One alternative is to use an aggregate file location for you filename.
Data never sleeps
Reeza
Super User

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...

 

Tom
Super User Tom
Super User

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;
wutao9999
Obsidian | Level 7

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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