Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use nested file - reading data from nested file

Status
Not open for further replies.

123sush

IS-IT--Management
Aug 20, 2008
7
US
Hi, I have 50 patients information and each is in saperate .txt file. The name of TXT file has a format like PAT00001.txt for 1st patient, PAT00002.TXT for second patient.. and this all file names are listed in another text file.

I need to read these patient information and study it.

To read individual patient file I can create data set for each patient and merge them so that all patient information comes in one data file. But that makes my program tooo.. big and time consuming.

So my question is do we have any thing in SAS,reads the name from txt file (containing all the names) and then go and read that txt file and create the data set. This will do in a DO loop for 50 times as we have 50 patient file names written in that file.
 
Yes, it's pretty simple actually, and doesn't even require a do loop.
Code:
* Specify filename statement which includes all the files*;
filename patients ("c:\PAT*.txt");

data all_patients;
  length filenm flnm $100;
  infile patients recfm=v ..... file=flnm;
  * File=filenm loads the filename into a temp variable names flnm *;

  length var1  $50
         var2  ...
         ;
  input var1
        var2
        ;
  * Load filename into a permanent variable which will be written out *;
  filenm = flnnm;
run;
You can use the SCAN function on the filename to extract components from the filename that you're interested in, i.e.
Code:
  filenm = scan(flnm,-1,'/\');
will get the filename without the path.

the filename statement as used above, can actually be used to list multiple files like this:-
Code:
filename ("c:\file1.txt","C:\file50.txt","H:\extras.txt");

There are other option that can be applied to the infile statement whih will gather other information from the file. In this way, it's possible to read in files in multiple formats and have condition input statements accordingly.
Enjoy.


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top