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.
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...
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.
Thank you for the assistance
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...
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.
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.
Thank you
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: