Online File

How to use this page


Rick Aster: Professional SAS Programming Logic: Contents

Chapter 16
Program
Reading a text file


*
    READ2.SAS
    Reads text file using I/O functions
    Rick Aster    May 2000
*;
DATA _NULL_;
   LENGTH RECORD MSG $ 75 FILEREF $ 8;
   RETAIN RECORD FILEREF '';
   FILE LOG;
*
   Define fileref.
*;
   RC = FILENAME(FILEREF, 'any.txt');
   IF RC > 0 THEN GOTO E1;
*
   Open file.
*;
   FID = FOPEN(FILEREF, 'S');
   IF NOT FID THEN GOTO E1;

*
   Process records.
*;
   DO WHILE (1);
      RC = FREAD(FID);
      IF RC THEN LEAVE; * Check for end of file or error;
      RC = FGET(FID, RECORD, 75);
      PUT RECORD $CHAR75.;
      END;

*
   Close file and clear fileref.
*;
   RC = FCLOSE(FID);
   RC = FILENAME(FILEREF);
   STOP;

*
   Error handling.
*;
E1:
   RC = SYSRC();
   PUT 'Error defining fileref or opening file. Return code ' RC;
   MSG = SYSMSG();
   IF MSG NE '' THEN PUT MSG;
   STOP;

 O /\

Global
Statements

RICK ASTER

SAS

BOOKS

Tech | Dictionary

Download | Rastinate

Rick Aster

Professional SAS Programming Logic

Contents/Online Files

Corrections

Catalog Page