Import CSV with SAS PROC IMPORT

A practical review of importing CSV files into SAS with PROC IMPORT: what the procedure changes, where type guessing and encoding cause trouble, and when an explicit DATA step is the safer choice.

PROC IMPORT is the quickest way to turn a comma-separated file into a SAS data set. You point it at a file, name an output table, and SAS works out the column names, types and lengths. That convenience depends on SAS guessing correctly. This review covers what the procedure changes, where meaning can be lost, and when an explicit DATA step is the better tool.

Scope note: this article draws on the documented behavior of PROC IMPORT and general CSV principles. No specific SAS release, platform or test file was supplied, so version-specific defaults and performance are not stated as measured facts. Check the documentation for your SAS release before relying on any default.

Is PROC IMPORT suitable for your CSV?

Bounded recommendation: use PROC IMPORT for exploration, one-off loads and files with clean, consistent columns. Its main benefit is speed. You get a usable data set without writing column definitions by hand.

The decisive tradeoff is control. PROC IMPORT infers each column's type and length from a sample of rows, so a value that appears only later in the file can be truncated or misread. A production job that runs on files that change from week to week can therefore produce different column definitions from one run to the next.

Prefer an explicit DATA step with INFILE and INPUT statements, the SAS equivalent of keeping the plain text and reading it on your own terms, when:

  • Identifiers such as ZIP codes, account numbers or product codes must keep their leading zeros.
  • The same job runs repeatedly and needs stable column types and lengths.
  • Long text values appear deep in the file rather than near the top.
  • Date columns mix formats or use a layout SAS might misread.
  • The output feeds a regulated or audited process where every conversion must be specified.

What PROC IMPORT actually changes

A CSV file is plain text: characters separated by delimiters and line breaks. PROC IMPORT does not edit that file. It reads it and writes a separate SAS data set in which each column has a name, a type (character or numeric), a length and possibly a format or informat. The meaning of a value can change at that point, not in the source file.

For delimited files, PROC IMPORT generates DATA step code and runs it, and it writes that code to the SAS log. Reading the log is the most direct way to see the decisions SAS made for each column.

A basic import looks like this:

proc import datafile="/path/to/sales.csv"
    out=work.sales
    dbms=csv
    replace;
    getnames=yes;
    guessingrows=max;
run;

The key statements and options do the following:

Option | What it controls
DATAFILE= | The path to the source text file
OUT= | The SAS data set to create
DBMS=CSV | Treats the file as comma-delimited; DBMS=TAB handles tab-delimited files and DBMS=DLM with DELIMITER= handles other separators
REPLACE | Overwrites an existing output data set of the same name
GETNAMES= | Whether the first row supplies column names
DATAROW= | The row where data begins, useful when header lines precede the data
GUESSINGROWS= | How many rows SAS scans to decide type and length; MAX scans the whole file at the cost of extra read time

Type guessing and the changes it can cause

If every sampled value in a column looks numeric, SAS makes the column numeric. A code stored as 01234 then becomes the number 1234, and the leading zero is gone from the data set. If the sample contains only short strings, the character length is set from those, and a longer value later in the file can be cut off. Raising GUESSINGROWS reduces the risk. Defining the column yourself removes it.

Column names

Header text is not always a valid SAS variable name. Depending on the VALIDVARNAME system option, spaces and special characters may be replaced with underscores, or the original text may be kept as a name literal. In both cases the label or name in SAS may not match the header exactly, which matters if downstream code refers to columns by name.

Quoted fields

The generated code reads comma-separated values with DSD-style handling, so a quoted value containing a comma, such as "Smith, Jane", is normally read as one field. Line breaks inside quoted fields are a common failure point because SAS may treat them as the end of a record. Inspect the result whenever the source contains free-text comments or addresses.

Will the imported data stay readable and usable?

Several factors decide whether the data set keeps the meaning of the source file.

Character encoding

If the file's encoding differs from the SAS session encoding, accented letters and other non-ASCII characters can appear garbled or fail to transcode. Declaring the encoding on a FILENAME statement and passing that fileref to PROC IMPORT makes the conversion explicit:

filename src "/path/to/customers.csv" encoding="utf-8";

proc import datafile=src
    out=work.customers
    dbms=csv
    replace;
    getnames=yes;
run;

Multi-byte characters also affect lengths. A character column sized in bytes may need more room than its visible character count suggests, and that can lead to truncation.

Line endings

A file created on Windows and read on Linux, or the reverse, can leave a stray carriage return at the end of the last field on each line. The TERMSTR= option on the FILENAME statement tells SAS which line terminator to expect.

Reproducibility and sharing

Because column definitions depend on file contents, two colleagues importing slightly different versions of a file can end up with incompatible data sets. A common, more stable approach is to copy the DATA step code PROC IMPORT writes to the log, fix the informats and lengths, and save that code as the import step. You keep the convenience of the first guess and gain a fixed definition.

An explicit version that protects a leading-zero code column might look like this:

data work.sales;
    infile "/path/to/sales.csv" dsd firstobs=2 truncover;
    length region $20 product_code $10;
    input region $ product_code $ units order_date :yymmdd10.;
    format order_date yymmdd10.;
run;

The column names, lengths and date informat above are illustrative. Match them to the actual layout of your file.

Checks after import

  • Read the log for notes about invalid data, truncation or lines that ran past the end of a record.
  • Run PROC CONTENTS to confirm each column's type and length.
  • Compare the row count against the source file.
  • Spot-check identifier columns for lost leading zeros and text columns for cut-off values.
  • Confirm that dates were read as SAS dates rather than character strings.

Gaps in this review

No test files, timings or release-specific defaults were available, so this review does not claim how quickly PROC IMPORT runs, how many rows it samples by default in a given release, or how it behaves in particular SAS environments such as SAS Studio, Enterprise Guide or SAS Viya. Accessibility and assistive-technology considerations do not apply to a data-loading procedure in the way they apply to displayed text. The relevant usability concern is whether downstream code and readers see accurate values.

Bottom line

PROC IMPORT with DBMS=CSV is a sound starting point and is often enough for clean, one-time loads. Its weakness is inference: types, lengths and names are guessed from the data, and identifiers, long text and dates can change meaning. When accuracy and repeatability matter, use the generated code as a draft and keep an explicit DATA step as the step of record.