SAS Infile Statement – Read raw data?

The most powerful and flexible feature of the SAS system, is it’s ability to read and write any kind of Raw file. SAS INFILE Statement and FILE statement acts as interface by identifying the source of the input data records (external files) to read (or) instream data. We can instruct SAS system to read the data that are already stored in your local computer using SAS INFILE statement.When working with large datasets, it is recommended to use INFILE statement rather than DATALINES statement.

Data does not always come in SAS datasets, we have to extract data from different external sources like MS Access, Excel spreadsheets, CSV or text files. To read the raw data with a datastep we use INFILE and INPUT statements.

  • INFILE :- The location of the file, the name of the file and the type of the file like CSV file or text file.
  • INPUT :- The list of fields to be read.

How to use SAS INFILE Statement

SAS INFILE statement is used to identify the filename of an external ASCII (text) file. It is more important that INFILE statement should be added after DATA statement and before the INPUT statement.We use INFILE statement in conditional processing like IF-THEN statement.

ADVERTISEMENT

SAS INFILE  syntax

INFILE file-specification <options&gt; <operating-environment-options&gt;;
INFILE DBMS-specifications;

File-specifications

  • external-file’ : – It specifies the physical name of an external file.
  • fileref :- Specifies the fileref of an external file.
  • fileref(file) :- Specifies a fileref of an aggregator storage location and the name of a file or member, enclosed in parentheses, that resides in the location.
  • Operating Environment Information :- Different operating environments call an aggregate grouping of files by different names, such as directory, a MACLIB, or a partitioned data set.

How to read external raw data using SAS INFILE statement?

SAS Infile Sample Program :-

Data prasanthdataset1;
Infile datalines;
input id name$ sex$ age sal;
Datalines;
001 Adarsh M 28 40000
002 Malli M 26 35000
003 Prasanth M 27 30000
;
Run;
Proc print data=prasanthdataset1 noobs;
title 'Customer details';
run;

Result:-

SAS INFILE statement sample program

In the above example, default delimiter is space so you no need to use any Infile options.

Reading Delimited data

DSD (delimiter-sensitive-data) :- It reads delimiter is comma and reads missing values with delimiter is a comma. SAS treats two consecutive delimiters as a missing values. It also removes double quotation marks from character values.

Sample program 2:-

Data Infileexample;
Infile datalines dsd;
input id name$ sex$ age sal;
Datalines;
001,Adarsh,M,28,40000
002,Malli,M,26,35000
003,Prasanth,M,27,30000
;
Run;

Proc print data=infileexample noobs;
Run;

Result:-

SAS INFILE statement example 2

In the above SAS INFILE statement example, there is a comma delimiter so you should use Infile options to read data without any error.