Read CSV in SAS with INFILE
How the SAS INFILE statement reads CSV and TSV files: header rows, DSD, delimiters, missing values, informats, and when PROC IMPORT or plain text is the better choice.
Reading a CSV file into SAS with a DATA step and the INFILE statement gives you explicit control over how each field becomes a variable. You decide the delimiter, the variable types, the lengths, and how dates and missing values are interpreted. This review covers what that conversion changes, where it can lose meaning, and when another route is the better fit. It is based on generally documented SAS behavior rather than on tests of a specific SAS release or platform. Option defaults and edge cases can differ between versions and operating environments, so confirm details against the documentation for your installation.
Is INFILE the right approach for your CSV?
INFILE is a good fit when the file layout is known and stable, and when types and lengths need to be the same on every run. The main benefit is predictability: SAS does not guess column types, so a code column with leading zeros stays character if you declare it that way. The decisive tradeoff is effort and maintenance. You name every variable yourself, and you have to update the INPUT statement whenever the file's columns change.
PROC IMPORT is often more practical for one-off exploration or for files whose structure you don't yet know. It can take variable names from the header row and infer types by scanning a number of rows, which you can raise with the GUESSINGROWS= option. Because it infers types, a column that looks numeric in the scanned rows but holds text later on can be typed wrongly. Leaving the data as plain CSV is preferable when the recipients don't use SAS, when the file must stay editable in a spreadsheet or text editor, or when nobody has yet agreed on the column meanings.
What the conversion actually changes
INFILE does not modify the CSV file. It points the DATA step at the file as an input source. The INPUT statement then parses each record into variables, and the result is stored as a SAS data set. The change happens in representation: free text becomes typed values. Numbers become numeric variables, text becomes fixed-length character variables, and dates read with a date informat become SAS date values, which are counts of days that need a format to display as dates. Any loss of meaning happens during this typing step, not in the source file.
Options that control the parsing
- DSD: treats two consecutive delimiters as a missing value, removes the quotation marks around quoted values, and allows delimiters inside quoted fields. It also makes the comma the default delimiter.
- DLM= (or DELIMITER=): sets the delimiter character. For a comma use DLM=','. For a tab-separated file use DLM='09'x.
- FIRSTOBS=2: starts reading at the second line, which skips a header row. INFILE does not use the header to name variables; you supply the names in the INPUT statement.
- TRUNCOVER or MISSOVER: stops SAS from jumping to the next line when a record has fewer fields than INPUT expects. Without one of these, a short line can pull values from the following record.
- LRECL=: sets the maximum record length, which matters for very wide rows.
- ENCODING= and TERMSTR=: set the character encoding and the line-ending convention. TERMSTR=CRLF is relevant when a file created on Windows is read on a Unix-like system.
A worked example
Here is an illustrative input file with a header row, a quoted field that contains a comma, and two empty fields:
order_id,region,product,order_date,quantity,price
1001,East,"Widget, large",2026-03-14,5,19.99
1002,West,,2026-03-15,,4.50
A DATA step that reads it with INFILE, using a fileref declared first:
filename src 'sales.csv' encoding='utf-8' termstr=crlf;
data work.sales;
infile src dsd dlm=',' firstobs=2 truncover;
length region $20 product $40;
informat order_date yymmdd10.;
format order_date date9.;
input order_id region $ product $ order_date quantity price;
run;
Given that input and the mechanism described above, the expected result is shown below. FIRSTOBS=2 skips the header. DSD keeps "Widget, large" as one value and drops its quotes. The empty product and quantity fields become a blank character value and a numeric missing value. Treat this as a reasoned expectation rather than a captured test run.
| order_id | region | product | order_date | quantity | price |
| 1001 | East | Widget, large | 14MAR2026 | 5 | 19.99 |
| 1002 | West | (blank) | 15MAR2026 | . (missing) | 4.5 |
The LENGTH statement matters. Under list input, a character variable without a declared length defaults to 8 bytes, so a longer product name would be cut off without warning. Declaring the lengths before INPUT prevents that.
Reading a TSV file instead
Tab-separated files use the same approach with a different delimiter. With DSD combined with DLM='09'x, two consecutive tabs are read as a missing value. Without DSD, SAS treats runs of delimiters as a single separator, which shifts later values into the wrong columns whenever a field is empty.
data work.sales_tsv;
infile 'sales.tsv' dsd dlm='09'x firstobs=2 truncover;
length region $20 product $40;
input order_id region $ product $ quantity price;
run;
Where meaning can be lost
- Leading zeros: identifiers such as US ZIP codes or account numbers lose their leading zeros if read as numeric. Read them as character.
- Truncation: character values longer than the declared or default length are cut off. Declare lengths explicitly.
- Dates: a date read without an informat is either rejected or stored as a meaningless number. Match the informat to the file's actual date format, and display the result with a date format.
- Embedded line breaks: a quoted field that contains a line break is usually treated as the end of the record, which splits one row into two. Such files may need cleaning before import or special handling. Verify the behavior on your own system.
- Encoding: accented or non-Latin characters can be garbled if the file's encoding differs from the session encoding. State the encoding explicitly.
- Header drift: INFILE maps columns by position, not by name. If the file supplier reorders or inserts columns, values land in the wrong variables without any error message.
Will the result stay usable when shared?
The SAS data set is convenient inside SAS, but other tools generally cannot open it directly. To share results with people who don't use SAS, export back to delimited text with PROC EXPORT, or with a DATA step that uses the FILE statement with DSD so values containing delimiters are quoted. Before relying on an exported file, check that dates appear in a readable format and that character identifiers kept their leading zeros. Spreadsheet applications may reinterpret these values when the file is opened, which depends on the application and its settings rather than on SAS. Keeping the original CSV alongside the data set leaves a plain-text reference that anyone can inspect and that can be re-read if the import logic changes.
Evidence gaps
No version-specific documentation, benchmarks or test runs were supplied for this review. The option behavior described here reflects commonly documented SAS conventions. Default record lengths, encoding defaults, and how quoted line breaks are handled can vary by release and platform. Before putting an import into regular use, run it against a sample file that contains your own edge cases (empty fields, quoted delimiters, long text, and non-ASCII characters) and check the SAS log for notes about invalid data or lines skipped to a new record.