SAS Reading Data

SAS has procedures for automating the import of data files, but sometimes it's necessary to manually read data from a text file.


Input

The primary way to read data into a SAS table is with a data step and the INFILE, INPUT, and OUTPUT statements.

InFile Statement

data LIBREF.TABLE;
    infile "data.txt" pad missover lrecl=80;
run;

The LRECL option specifies the fixed-width size of a record. The MISSOVER and PAD options together ensure that unspecified fields are set to missing.

Input Statement

data LIBREF.TABLE;
    infile "data.txt" pad missover lrecl=80;
    input
        RECTYPE  $ 1-1 @
    ;
        
    if RECTYPE = "1" then do;
        input
            RECTYPE  $ 1-1
            VAR1       2-5
        ;
        output LIBREF.TABLE1;
    end;
    else RECTYPE = "2" then do;
        input
            RECTYPE  $ 1-1
            VAR1       2-2
        ;
        output LIBREF.TABLE2;
    end;
run;

As demonstrated above, records can be partially input, processed, then completely input later.


CategoryRicottone

SAS/ReadingData (last edited 2023-03-30 20:41:10 by DominicRicottone)