Working with a Flat File in SAS – INFILE Statement

Welcome back to Mindful Data Minds! In this session, we’ll learn how to work with flat files in SAS using the INFILE statement.

What is a Flat File?

A flat file is a simple database stored in a file without indexing or complex structure. Examples:

  • .txt (text file)
  • .csv (comma-separated values file)

Excel files (.xlsx, .xlsm) are not flat files because they have structured formatting.

Watch the Full Tutorial

What You Will Learn

  • What a flat file is.
  • How to use INFILE to read text and CSV files.
  • How to handle space, comma, and tab delimiters.
  • How to import fixed-width files.
  • How to use INFILE options like MISSOVER, TRUNCOVER, and STOPOVER.

Step 1: Importing a Flat File

To read a flat file, use the INFILE statement:

data a;
   infile "C:\tutorial\file1.txt";
   input emp_id name :$10. desig :$10. city :$10.;
run;

Explanation:

  • infile → Points to the file path.
  • input → Defines variables (numeric or character with $).
  • run; → Executes the step.

Step 2: Handling Delimiters

Flat files often use delimiters (space, comma, tab, etc.).

Space-Delimited File

Works directly with input as shown above.

CSV File (Comma-Separated)

data b;
   infile "C:\tutorial\file2.csv" dlm=',' dsd;
   input emp_id name :$10. desig :$10. city :$10. $;
run;
  • dlm=’,’ → Specifies comma as delimiter.
  • dsd → Handles missing values correctly.

Tab-Delimited File

Tab is not a standard character, so use its hexadecimal value ('09'x):

data c;
   infile "C:\tutorial\file3.txt" dlm='09'x dsd;
   input emp_id name :$10. desig :$10. city :$10.;
run;

Step 3: Fixed-Width Files

Some flat files store data in fixed positions (columns). Example:

  • Rank → position 1–2
  • First Name → position 3–13
  • Last Name → position 14–24
  • Occupation → position 25–46
  • Nationality → position 47 onwards
data d;
   infile "C:\tutorial\file4.txt" ;
   input rank first_name $3-13 last_name $14-24 occupation $25-46 nationality $50.;
run;

Step 4: INFILE Options

SAS provides options to handle tricky cases:

  • MISSOVER → Prevents SAS from reading the next line if data is missing. Missing values are set to blank.
  • TRUNCOVER → Reads until the end of the line, ensuring no values are skipped.
  • STOPOVER → Stops the program with an error if data is missing. Useful when missing values are not allowed.

Example with MISSOVER

data e;
   infile "C:\tutorial\file4.txt" missover;
   input rank first_name $3-13 last_name $14-24 occupation $25-46 nationality $50;
run;

Ensures all values (including nationality) are read correctly.

Example with TRUNCOVER

data f;
   infile "C:\tutorial\file4.txt" truncover;
   input rank first_name $3-13 last_name $14-24 occupation $25-46 nationality $50.;
run;

Ensures all values (including nationality) are read correctly.

Example with STOPOVER

data g;
   infile "C:\tutorial\file4.txt" stopover;
   input rank first_name $3-13 last_name $14-24 occupation $25-46 nationality $50.;
run;

Download Section

Next Step

Continue learning by exploring the next tutorial in this series. Also subscribe to get notified about new lessons.

Have a Question?

Drop your doubts in the comments below or contact us.

Leave a Reply

Your email address will not be published. Required fields are marked *