Mastering the INPUT and INFILE Statements in SAS: A Beginner's Guide with Examples

Introduction

If you're just starting with SAS programming, one of the most essential skills you'll need is the ability to read raw data files into SAS datasets. This is where the INPUT and INFILE statements come into play. These two statements are fundamental when you're working with external data sources like .txt, .csv, or .dat files.

In this post, we'll explore:

  • What the INFILE and INPUT statements do
  • Syntax and options
  • Step-by-step examples
  • Common errors and best practices

Whether you're preparing for the Base SAS Certification or learning SAS for data analytics, this guide will help you understand how to efficiently read data into SAS.


What is the INFILE Statement in SAS?

The INFILE statement is used to tell SAS where to find your external data file. It defines the file path and other important file-reading options such as delimiters and record length.

Syntax:

INFILE 'file-path' <options>;

Example:

INFILE 'C:\Data\students.txt' DLM=',' FIRSTOBS=2;

  • DLM=',' — Specifies the delimiter as a comma.
  • FIRSTOBS=2 — Tells SAS to start reading from the second row (often used to skip headers).


What is the INPUT Statement in SAS?

The INPUT statement tells SAS how to read the data — the structure, variable names, types, and formats. It works hand-in-hand with the INFILE statement to read external files into a SAS dataset.

Syntax:

INPUT var1 $ var2 var3;

  • Use $ for character variables.
  • No $ is needed for numeric variables.

Example:

INPUT Name $ Age Height;

Example: Reading a CSV File Using INFILE and INPUT

Let's say you have a file named students.csv with the following data:

Name,Age,Score
Alice,22,88 Bob,23,91 Charlie,21,85

SAS Code:

DATA student_data;
INFILE 'C:\Users\YourName\Documents\students.csv' DLM=',' FIRSTOBS=2; INPUT Name $ Age Score; RUN; PROC PRINT DATA=student_data; RUN;

More on Input and Infile Statement - 

1. 
Data <Dataset Name>
Infile 'Location\Filename' DLM=<delimiter> Firstobs=<Starting Position>;
Input <Variable Name and formats>;
run;

2. 
FileName <Name/Alias for File> 'Location/FileName';
Data <Dataset Name>
Infile above defined Name/Alias for File  DLM=<delimiter> Firstobs=<Starting Position>;;
Input <Variable Name and formats>;
run;

Here FileName is a Global Statement

For Example - I have a Text file on my location - /home/../New Folder/New Text Document.txt with Credit Card Number and Spend information, to import that
1.
Data datahark;
infile  '/home/../New Folder/New Text Document.txt' Firstobs=2;
INput cc Spend;
run;

2.
Filename CC '/home/../New Folder/New Text Document.txt';
Data datahark;
infile  cc Firstobs=2;
INput cc Spend;
run;

Both of above code will import the File and Give the same output - 

Use of Infile and Input Statement in SAS

Important Options in INFILE

OptionDescription
DLM=','Sets the delimiter (comma, space, tab, etc.)
FIRSTOBS=nReads from the nth record (skip headers)
DSDHandles missing values and quotes in CSV
MISSOVERPrevents reading the next line if data is missing
TRUNCOVERAvoids errors when fewer columns exist than expected

Reading Space-Delimited Data

DATA test_scores;
INFILE 'C:\Data\testdata.txt'; INPUT StudentID $ Subject $ Score; RUN;

This reads a space-separated file without needing DLM.


Common Mistakes to Avoid

  1. File Not Found: Always use the full path or set the correct working directory.
  2. Wrong Delimiter: Use the correct delimiter option in INFILE.
  3. Incorrect Data Types: Forgetting to use $ for character variables.
  4. Header Row Issues: Use FIRSTOBS=2 if your data has a header.


Best Practices

  • Use DSD and FIRSTOBS=2 for clean CSV handling.
  • Always validate data using PROC PRINT after loading.
  • Modularize your code: Keep data import in a separate step.

Conclusion

The INFILE and INPUT statements in SAS are powerful tools for importing data from external files. Mastering them will open the door to working with real-world datasets in your SAS projects.

By understanding how these statements work and how to apply the right options, you’ll be equipped to handle complex data import tasks efficiently. Practice with different file formats and structures to strengthen your data-handling skills.

Post a Comment

0 Comments