Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 37 ยท Reading and Writing Data

Writing Data with FILE, PUT and PROC EXPORT

Write data out of SAS: DATA _NULL_ with FILE and PUT for custom text and CSV files, writing to the log, PROC EXPORT for CSV and Excel, and saving permanent data sets.

Reading data is half the job; the other half is getting results out: a CSV for a colleague, a fixed-width file for another system, a message in the log. SAS gives you PROC EXPORT for quick exports and the FILE and PUT statements for full control. This lesson also shows how to save data sets permanently.

DATA NULL with FILE and PUT

FILE is the output twin of INFILE, and PUT is the twin of INPUT. The data set name _null_ says "run the step, but do not create a data set":

SAS
data _null_;
   set sashelp.class;
   file "C:\training\out\class.txt";
   put name age height weight;
run;

Each observation becomes one line of text, with values separated by a blank. The path is an example; the folder must exist.

Controlling the layout

PUT accepts pointers and formats just as INPUT does:

SAS
data _null_;
   set sashelp.class;
   file "C:\training\out\class_fixed.txt";
   put @1 name $10. @12 age 3. @16 weight 6.1;
run;

Each field starts at a fixed column, giving a fixed-width file. Common pointer controls are @n (go to column n), +n (skip n columns), / (start a new line) and #n (go to line n of the record).

Writing a CSV by hand

SAS
data _null_;
   set sashelp.class;
   file "C:\training\out\class.csv" dsd dlm="," lrecl=200;
   if _n_ = 1 then put "Name,Age,Weight";   /* header line */
   put name age weight;
run;

With dsd on the file statement, SAS inserts the commas, quotes any value that contains a comma and writes missing values as empty fields. lrecl= makes sure long lines are not cut. Handling the header with if _n_ = 1 is a standard pattern.

Writing text to the log and the results

Statement Destination
put ...; (no file) The log
putlog ...; Always the log
file print; put ...; The results/output window
file "path"; put ...; An external file
file log; Switch back to the log

Useful PUT shortcuts:

SAS
put _all_;              /* every variable with its name and value */
put "Row " _n_ "has age " age;
put name= age=;         /* named output: name=Alfred age=14 */

The named output form (name=) is the fastest way to debug. Putting a variable name with an equals sign makes the log show both name and value. Together with if conditions, you can print only interesting rows.

A small custom report

SAS
data _null_;
   set sashelp.class (obs=5);
   file print;
   if _n_ = 1 then put "Name        Age   Weight";
   put name $10. +2 age 3. +3 weight 6.1;
run;

Here +2 skips two columns. A custom report like this gives you full control over spacing, and the same program can produce text for another program to read. For polished reports, ODS and PROC REPORT (in the Statistics and Reporting chapter) are usually better.

PROC EXPORT

PROC EXPORT is the mirror image of PROC IMPORT:

SAS
proc export data=sashelp.class
            outfile="C:\training\out\class.csv"
            dbms=csv
            replace;
run;

It writes a header row of variable names by default. Use putnames=no; inside the step to leave it out. For Excel use dbms=xlsx and name the sheet:

SAS
proc export data=sashelp.class
            outfile="C:\training\out\class.xlsx"
            dbms=xlsx replace;
   sheet="Class";
run;

Notes:

  • By default PROC EXPORT writes formatted values for formats such as date9., so a date appears as 15MAR2024 in the CSV rather than as a number. That is usually what you want, but be careful if another program expects ISO dates. Apply a format first (format d yymmdd10.;) or build the file with put.
  • Rows and columns are written in data set order. Use keep or where options on the input to choose them.
  • Excel files cannot hold more than 1,048,576 rows per sheet.

Saving permanent data sets

The simplest output is a data set in a permanent library. Anything written to a library you assigned with libname stays after the session ends:

SAS
libname out "C:\training\out";

data out.class_teens;
   set sashelp.class;
   where age >= 13;
run;

Besides .sas7bdat, you can write to other engines: libname xl xlsx "file.xlsx"; data xl.teens; set ...; run; creates a worksheet, and libname js json "file.json"; writes JSON, subject to license and version. Use proc copy to copy whole libraries, proc datasets to rename, delete and modify data sets, and proc append to add rows.

A writer model

Python is not SAS, but the stand-in below mirrors the pattern of file ...; put ...; with a header row and fixed columns, so you can compare layouts.

Python Python stand-in: PUT with a header, fixed columns and CSV

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Before sending an exported file anywhere, open it in a text editor. Check the header, the date layout, the delimiter and the treatment of missing values.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does 'data null;' mean?

    null lets a DATA step run for side effects such as writing files.
  2. 2. Where does a PUT statement write when there is no FILE statement?

    By default put writes to the log.
  3. 3. What does 'put name= age=;' print?

    The named output form prints each variable's name, an equals sign and its value.
  4. 4. How do you write a header only on the first line in a DATA NULL step?

    N is 1 only in the first iteration.

Summary

  • data _null_; set ...; file "path"; put ...; run; writes custom text, fixed-width or CSV files; dsd dlm="," on file gives correct CSV.
  • Without file, put writes to the log; file print sends text to the results; put name=; is the quickest debug tool.
  • proc export writes CSV and Excel quickly, using formatted values by default.
  • Save permanent data sets by writing to a library assigned with libname.

Next: Subsetting and Selecting Data: WHERE, IF and DELETE