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

Dear SAS Users

I'm asked to research on "how to read a text file in SAS when a folder name is known but the file name is unknown". I checked all my notes but nothing is coming from there.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Is this some sort of learning exercise?  You can get a directory listing (assuming you have OS access):

filename tmp pipe 'dir "c:\path\*.txt" /b';

data want;
  length tmp $200;
  infile tmp;
  input tmp $;
run;

Will get you a list of files in that folder with .txt extension.  Its really a very daft question however.  If you don't know what to expect, how can you program for it?  Is it a delimited file, a fixed format file, or something else.  What structure will it be in?  What if its xml or Json?  What if its not .txt extension but csv?  Its a bit of a daft question, you need to fix in certain paramters and such like before you even look at coding.  I.e. file transfer specs, what file, when, what structure, via what medium etc.  Process documentation, how is file to be read, to what structure, where to store etc.  Once documentation is in place, then code.

If of course its a learning question... 

View solution in original post

6 REPLIES 6
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

you could use members to create a SAS dataset list of files that are in the folder.  Then use infile within a data step and read in all those text files into SAS datasets.  You should request a record layout for the text files so that the information is consistence for interpretation.

ZosTa
Calcite | Level 5

Thank you for the assistance

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Is this some sort of learning exercise?  You can get a directory listing (assuming you have OS access):

filename tmp pipe 'dir "c:\path\*.txt" /b';

data want;
  length tmp $200;
  infile tmp;
  input tmp $;
run;

Will get you a list of files in that folder with .txt extension.  Its really a very daft question however.  If you don't know what to expect, how can you program for it?  Is it a delimited file, a fixed format file, or something else.  What structure will it be in?  What if its xml or Json?  What if its not .txt extension but csv?  Its a bit of a daft question, you need to fix in certain paramters and such like before you even look at coding.  I.e. file transfer specs, what file, when, what structure, via what medium etc.  Process documentation, how is file to be read, to what structure, where to store etc.  Once documentation is in place, then code.

If of course its a learning question... 

ZosTa
Calcite | Level 5

This works in a way, it only displays the names of the files. I believe the next move is to write a code that imports the files and create datasets, am I correct?  Please explain the "tmp" in the code.

Thank you for the response.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Tmp is just short for temporary, could be called anything you like.  I was only answering your question:

"I'm asked to research on "how to read a text file in SAS when a folder name is known but the file name is unknown".

That is how you get a list of filenames, from there its up to you how you import them.

 

What I will say, again, is that you cannot base an process on something which is unknown, it will not work.  Refer to my previous post on the reasons why.

ZosTa
Calcite | Level 5

Thank you

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 916 views
  • 0 likes
  • 3 in conversation