Online File

How to use this page


Rick Aster: Professional SAS Programming Logic: Contents

Chapter 16
Program
Reading a SAS dataset


*
    read1.sas
    Reads SAS dataset using I/O functions
    Rick Aster    May 2000
*;
DATA _NULL_;
   LENGTH NAME $ 60 MSG $ 75;
   FILE LOG;
*
   Define libref.
*;
   RC = LIBNAME('MAIN1', 'main');
   IF RC > 0 THEN GOTO E1;
*
   Open SAS dataset.
*;
   DSID = OPEN('MAIN1.LIST');
   IF NOT DSID THEN GOTO E2;
*
   Find variables NAME and NUMBER.
*;
   NAME_VAR = VARNUM(DSID, 'NAME');
   NUM_VAR = VARNUM(DSID, 'NUMBER');
   IF NOT (NAME_VAR AND NUM_VAR) THEN GOTO E3;
   IF NOT (VARTYPE(DSID, NAME_VAR) = 'C' AND VARTYPE(DSID, NUM_VAR) = 'N')
       THEN GOTO E4;

*
   Process observations.
*;
   DO WHILE (1);
      RC = FETCH(DSID);
      IF RC THEN LEAVE; * Check for end of file or error;
      NAME = GETVARC(DSID, NAME_VAR);
      NUMBER = GETVARN(DSID, NUM_VAR);
      PUT NUMBER NAME;
      END;

*
   Close SAS dataset and clear libref.
*;
CLOSE:
   RC = CLOSE(DSID);
   RC = LIBNAME('MAIN1');
   STOP;

*
   Error handling.
*;
E1:
   PUT 'Unable to find main library. Return code ' RC;
   MSG = SYSMSG();
   IF MSG NE '' THEN PUT MSG;
   STOP;

E2:
   RC = SYSRC();
   PUT 'Unable to open LIST member. Return code ' RC;
   MSG = SYSMSG();
   IF MSG NE '' THEN PUT MSG;
   STOP;

E3:
   PUT 'Variables NAME and NUMBER not found.';
   GOTO CLOSE;

E4:
   PUT 'Wrong data type for variable NAME or NUMBER.';
   GOTO CLOSE;
RUN;

 O /\

Global
Statements

RICK ASTER

SAS

BOOKS

Tech | Dictionary

Download | Rastinate

Rick Aster

Professional SAS Programming Logic

Contents/Online Files

Corrections

Catalog Page