Export CSV from SAS

How to export CSV in SAS with PROC EXPORT, a DATA step or ODS CSV, what the conversion changes in your data, and what to check before sharing the file.

What exporting a CSV from SAS involves

A SAS dataset stores typed variables (numeric or character) along with metadata such as formats, labels and lengths. A CSV file is plain text: one line per row, with values separated by commas. Exporting converts each observation into a line of text and drops most of the metadata. SAS offers three common routes: the EXPORT procedure, a DATA step that writes the file directly with FILE and PUT statements, and the ODS CSV destination, which captures procedure output as comma-separated text. They differ in how much control you get and in what ends up in the file.

Is CSV the right output for your use?

CSV is a sound choice when the receiving system needs a simple table that almost any tool can read: database loaders, scripts, other statistical packages, or version-controlled data snapshots. Its main benefit is portability. The decisive tradeoff is that the file carries no variable types, formats or labels, so the consumer has to reinterpret every column, and some tools will guess wrongly. If the data is going back into SAS, keeping it as a SAS dataset avoids that loss entirely. If the recipient will only open the file in a spreadsheet and needs columns to keep their types, a native spreadsheet export may serve them better, although writing XLSX from SAS typically depends on the SAS/ACCESS Interface to PC Files being licensed. Plain CSV remains the better option when the file will be processed by automated pipelines, compared line by line, or read by tools you do not control.

Option 1: PROC EXPORT

PROC EXPORT is the shortest route and suits most whole-dataset exports. The example below writes the SASHELP.CLASS sample dataset to a CSV file. Replace the path with one that is valid on the machine where SAS runs.

proc export data=sashelp.class
    outfile="/path/to/class.csv"
    dbms=csv
    replace;
    putnames=yes;
run;

REPLACE allows SAS to overwrite an existing file; without it, the export stops if the file already exists. PUTNAMES=YES writes variable names as the first row, which is the default for delimited output. Use the LABEL option on the PROC EXPORT statement if you want variable labels instead of names in the header row. PROC EXPORT generates a DATA step behind the scenes and writes it to the log. That generated code is worth reading, because it shows which formats are applied to each column and therefore exactly how the values will appear in the file.

Option 2: DATA step with FILE and PUT

Writing the file yourself takes more code but gives full control over column order, header text, encoding and line endings.

data _null_;
    file "/path/to/class.csv" dsd dlm=',' encoding='utf-8' lrecl=32767;
    set sashelp.class;
    if _n_ = 1 then put 'Name,Sex,Age,Height,Weight';
    put name sex age height weight;
run;

The DSD option makes SAS separate values with the delimiter, wrap a value in double quotes when it contains the delimiter, and double any embedded quotation marks. ENCODING= sets the character encoding of the output file instead of relying on the session default. LRECL= raises the maximum line length so wide rows are not truncated or wrapped. The TERMSTR= option on the same FILE statement controls line endings, for example TERMSTR=CRLF when a Windows-oriented consumer expects them.

Option 3: ODS CSV

ODS CSV captures the output of a procedure rather than the dataset itself. It is useful when the table you want to share is a procedure result, such as a summary or a frequency table.

ods csv file="/path/to/class_report.csv";
proc print data=sashelp.class noobs;
run;
ods csv close;

Because the file mirrors the procedure's layout, details such as an observation-number column (suppressed here with NOOBS), titles and quoting conventions can differ from what PROC EXPORT produces. Inspect the file before treating it as a clean data table. Remember to close the destination, or later output in the session will keep being written to the file.

Choosing between the three methods

Method | Best for | Control | Main caution
PROC EXPORT | Exporting a whole dataset quickly | Moderate: header names or labels, replace behavior | Formats attached to variables shape the output text
DATA step with FILE and PUT | Exact layout, encoding and line endings | Full | You maintain the header row and column list yourself
ODS CSV | Sharing procedure results as a table | Limited to the procedure's output | Layout and quoting follow the report, not the raw data

What the export actually changes

The source dataset is not modified; the CSV is a separate text representation of it. Nothing in that representation is visual-only. Unlike a display format inside SAS, whatever text lands in the file is exactly what every consumer receives. That has several consequences:

  • Numbers become text shaped by their formats. A format that shows two decimal places writes two decimal places, so rounding that was only cosmetic inside SAS becomes real in the file.
  • SAS dates are stored as the number of days since January 1, 1960. A date variable without a date format is written as that raw number, not as a readable date.
  • Character values containing commas or quotation marks are quoted, with embedded quotes doubled, when DSD-style output is used. Readers that do not follow standard CSV quoting may split those fields incorrectly.
  • Missing numeric values are written using the character set by the MISSING= system option, which defaults to a period. Some consumers will read that period as text rather than as an empty value.
  • Header text comes from variable names or labels, depending on the options you choose.
  • Characters outside basic ASCII depend on the output encoding. A mismatch between the encoding SAS writes and the encoding the reader assumes produces garbled text.

Will the file stay readable and usable after sharing?

The CSV text itself is stable once copied or transferred. The risk lies in how each destination interprets it. Spreadsheet applications commonly guess column types when a CSV is opened directly. That can strip leading zeros from ID codes, show long digit strings in scientific notation, or turn date-like strings into dates. If someone saves the file again from the spreadsheet, those changes become permanent. No tests of specific applications or versions were supplied for this review, so confirm the behavior in the tool your recipients actually use. Importing with explicit column types, instead of opening the file directly, avoids most of these conversions.

Applications also differ in whether they need a byte-order mark to recognize UTF-8, and no documentation for particular destinations was supplied here. If your data contains accented letters or other non-ASCII characters, test a small sample file in the target application before sending the full export.

Where the file is written matters too. In browser-based environments such as SAS Studio, OUTFILE= and FILE paths refer to the SAS server's file system, not your local computer. You will need to download the file afterward, and the path must be one that the SAS session has permission to write to.

For accessibility, a CSV has no structural markup beyond its first row. How a screen reader presents the data depends on the application used to open it, and no assistive-technology testing was supplied for this review. Clear, descriptive header names help regardless of the tool. If people need to read the data rather than process it, a properly marked-up table in a document or report may be more usable than a raw CSV.

Checks before you share the file

  • Open the CSV in a plain-text editor and confirm the header row, delimiters and quoting.
  • Check that date columns are formatted as dates rather than day counts.
  • Confirm that numeric precision is what you intend to publish, not just what the display format showed.
  • Look at how missing values appear and whether the consumer will interpret them correctly.
  • Verify the encoding and line endings against what the receiving system expects.
  • Compare row counts between the dataset and the file.
  • Test one sample file in the recipient's actual tool before distributing widely.

Evidence gaps

This review describes generally documented SAS behavior. It is not based on test runs, version-specific documentation or measurements of particular destination applications, and none were supplied. Defaults can vary by SAS release and session settings, so confirm option behavior in the documentation for your release and validate a sample export before relying on it.