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:
Example:
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:
- Use
$ for character variables. - No
$ is needed for numeric variables.
Example:
Example: Reading a CSV File Using INFILE and INPUT
Let's say you have a file named students.csv with the following data:
SAS Code:
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 -
Important Options in INFILE
| Option | Description |
|---|
DLM=',' | Sets the delimiter (comma, space, tab, etc.) |
FIRSTOBS=n | Reads from the nth record (skip headers) |
DSD | Handles missing values and quotes in CSV |
MISSOVER | Prevents reading the next line if data is missing |
TRUNCOVER | Avoids errors when fewer columns exist than expected |
Reading Space-Delimited Data
This reads a space-separated file without needing DLM.
Common Mistakes to Avoid
- File Not Found: Always use the full path or set the correct working directory.
- Wrong Delimiter: Use the correct delimiter option in INFILE.
- Incorrect Data Types: Forgetting to use
$ for character variables. - 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.
Click here to Read more »Labels: About SAS, Base SAS, Datahark, External Data in SAS, INFILE Statement in SAS, INPUT Statement in SAS, Reading Data in SAS, SAS, SAS CSV Import, SAS File Handling, SAS Programming