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 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;
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
andFIRSTOBS=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.
0 Comments
If you have any doubt please comment or write us to - datahark12@gmail.com