All YOUR Questions Answered at
[sas.answers @ Holland Numerics]
WebRing
Sponsored Links for SAS Software Users
Careers, Jobs and Contracts, Services and Products

Frequently Asked Questions about SAS Software

The following Question & Answer List is based on SAS questions received by the author.

Reading Flat Files

Question Answer
I have a changing number of files with similar names and the same data layout that I want to be able to read into a single DATA step. Is there an easy way to read them all without necessarily knowing all their names and how many exist? In SAS version 6.12 and above for Windows and UNIX you can use the wildcard character '*' in the file specification for FILENAME or INFILE, so if all you files begin with the same text the code will read them all, e.g.:
FILENAME indata 'file*.dat';
 
DATA sasdata1;
  INFILE indata;
  INPUT column1 column2;
  /* more code ... */
RUN;
 
DATA sasdata2;
  INFILE 'data*.txt';
  INPUT column1 column2;
  /* more code ... */
RUN;
I know which records I want to extract, but how do I split a flat file into several separate files using SAS Software? You can now use the special SAS variables _N_ (input record counter) and _INFILE_ (input record text buffer) to select and copy, respectively, the input records without any changes, e.g.:
FILENAME outdata1 'fileout1.dat';
FILENAME outdata2 'fileout2.dat';
 
DATA _NULL_;
  INFILE 'filein.dat';
  INPUT;
  IF 23 <= _N_ < 45 THEN DO;
    FILE outdata1;
    PUT _INFILE_;
  END;
  ELSE IF 45 <= _N_ < 100 THEN DO;
    FILE outdata2;
    PUT _INFILE_;
  END;
RUN;
Can I read multiple flat files into a single DATA step? Yes, try the following sample code which reads the 3 flat files sequentially:
FILENAME indata
 ('indata1.dat', 'indata2.dat', 'indata3.dat');
 
DATA sasdata;
  INFILE indata;
  INPUT .........;
  .......;
RUN;
How do you find a specific string in a long input record, then read the data immediately afterwards? Try the following sample code which looks for the first occurrence of the strings 'MONDAY' and 'TUESDAY', then reads the number following them in 2 different ways:
FILENAME indata 'indata1.dat';
 
DATA sasdata;
  INFILE indata;
  dayname='MONDAY';
  INPUT @dayname number1 8.
        @'TUESDAY' number2 8.;
  .......;
RUN;

     Back to Main FAQ Menu
Number of visitors = Counter (since 15th May 2000)

Email: Phil Holland <phil.holland@bcs.org.uk>

Web Design by Holland Numerics

Valid HTML 4.01!