Read CSV with SAS INFILE

How a SAS DATA step with INFILE reads CSV and TSV files, which options protect the data's meaning, where values can silently change, and when PROC IMPORT or plain text is the better choice.

Reading a comma-separated file into SAS with a DATA step and an INFILE statement turns delimited text into a typed SAS data set. The method gives you precise control over every column, but that control comes with responsibility. If lengths, informats and options are set carelessly, the result can look correct while values have been truncated, reformatted or shifted into the wrong columns. This review explains what the transformation does, where meaning can change, and when another approach makes more sense.

Is INFILE the right way to read your CSV?

An INFILE-based DATA step is a good fit when you need repeatable, explicit control over column names, types, lengths and date or number handling. This matters most for production jobs, files that arrive on a schedule, and data where identifiers or codes must survive exactly as written. The main benefit is that nothing is guessed: you declare how each field is read. The decisive tradeoff is effort and exposure to mistakes. You must know the file layout in advance, and an error in your declarations can damage values without stopping the step.

PROC IMPORT is often more practical for one-off exploration of an unfamiliar file, because it infers types and lengths for you. That inference comes from a sample of rows, so it can still mis-type or truncate columns. Leaving the data as plain CSV is preferable when you only need to view it, pass it to a tool that is not SAS, or keep an unaltered record of what was received. Reading the file into SAS does not modify the source file, so keeping the original CSV alongside the SAS data set costs nothing and preserves a reference copy.

What the DATA step actually changes

The source file is not altered. The DATA step reads the stored characters of each record, splits them at the delimiter, and converts each piece according to the variable type and informat you specify. Character fields become fixed-length character variables, measured in bytes. Numeric fields become floating-point numbers. Dates become numeric day counts that only look like dates when a format is applied. These conversions change how the values are stored, so the output can differ from the text in ways that matter.

data work.sales;
  infile 'sales.csv' dsd dlm=',' firstobs=2 truncover;
  length region $20 product $40 customer_id $10;
  input region $ product $ customer_id $
        units
        price     :comma12.
        sale_date :yymmdd10.;
  format sale_date yymmdd10.;
run;
  • DSD: treats two consecutive delimiters as a missing value, removes the quotes around quoted values, and keeps delimiters that appear inside quotes as part of the value. When DSD is used, comma is the default delimiter.
  • DLM=',': names the delimiter explicitly. With DSD this is redundant for commas, but it makes the intent clear.
  • FIRSTOBS=2: skips the header row so column names are not read as data.
  • TRUNCOVER: if a record ends early, the remaining variables are set to missing instead of SAS reading values from the next line.
  • LENGTH: sets character variable lengths before INPUT. Without it, character variables read with list input default to 8 bytes.
  • Colon modifier (for example :comma12.): applies an informat while still reading the field as a delimited value, so the informat width does not cause SAS to read past the delimiter.

Where meaning can change without an error

  • Truncated text: a character value longer than its declared length is cut off. Because lengths are counted in bytes, UTF-8 text with accented letters or non-Latin scripts needs more room than its character count suggests, since one character can take up to four bytes.
  • Lost leading zeros: identifiers such as postal codes or account numbers become numbers if they are read as numeric, and leading zeros disappear. Read them as character.
  • Precision loss: SAS numbers are stored as 8-byte floating point, which on most platforms holds roughly 15 to 16 significant digits. Long numeric identifiers can be silently rounded and should be read as character.
  • Date misreads: an informat that does not match the file's date pattern produces missing values or notes about invalid data. Day-month and month-day orders need the correct informat.
  • Column shifts: without DSD, consecutive commas are treated as a single delimiter, so an empty field causes every later value in that row to move one column to the left.
  • Line endings: a file with Windows line endings read on a UNIX-like system can leave a carriage return at the end of the last field. The TERMSTR=CRLF option addresses this.
  • Line breaks inside quoted fields: INFILE reads one record per line, so a quoted value that contains a line break splits the row. If the file ends records with CRLF and uses bare LF inside fields, TERMSTR=CRLF can keep those rows intact. Otherwise the file may need cleaning before it is read.
  • Encoding: if the file's encoding differs from the SAS session's encoding, characters can be mistranslated. The ENCODING= option on INFILE declares the file's encoding, but a session that cannot represent a character still cannot store it correctly.

Reading tab-separated files

TSV files use the same approach. Replace the delimiter with the hexadecimal code for a tab. Keep DSD if empty fields can occur, so that two adjacent tabs are read as a missing value.

infile 'sales.tsv' dsd dlm='09'x firstobs=2 truncover;

INFILE DATA step versus PROC IMPORT

Aspect | DATA step with INFILE | PROC IMPORT
Column types and lengths | Declared by you | Inferred from sampled rows
Repeatability | Same result whenever the layout is unchanged | Can change if new data alters the inference
Setup effort | Higher, layout must be known | Lower
Risk of silent truncation | Comes from your declared lengths | Comes from inference on a limited sample
Best fit | Scheduled or production loads, sensitive identifiers | Quick exploration of unfamiliar files

Will the result stay usable after you share it?

A SAS data set is not plain text. Someone without SAS, or without a tool that reads SAS data sets, cannot open it directly. To share results as text, export them again, for example with PROC EXPORT or a DATA step that uses FILE with the DSD option. Check that formats are applied as intended, because dates and formatted numbers are written according to their formats. When you share the reading code, avoid copying it through word processors or chat tools that turn straight quotes into curly quotes. SAS does not treat curly quotes as string delimiters, so the pasted code will fail.

Before relying on the output, read the SAS log. Notes such as "Invalid data for", "LOST CARD", or "SAS went to a new line when INPUT statement reached past the end of a line" indicate that the file did not match your declarations. Comparing the row count with the number of data lines in the file, and checking the maximum length of key character columns, helps catch truncation and split rows.

Gaps in this review

No sample file, SAS version or test results were supplied, so this review describes documented, general INFILE behavior rather than observed results in a particular environment. Encoding handling, default session settings and how a byte order mark at the start of a UTF-8 file is treated can vary by SAS release and configuration. Confirm these points against the documentation for your installation and with a small test file that includes empty fields, quoted commas, non-ASCII text and long identifiers.