SAVE SAS OUTPUT AS PDF
SAS How To's

How to Save SAS Output as a PDF File

Many SAS procedures generate output, like tables, summaries, or graphs. By default, SAS only displays these outputs in the Results Viewer. Fortunately, SAS can also save the output in other formats, such as HTML, Excel, or PDF.

If you want to save your SAS output as a PDF file, you need to write your SAS code between two ODS statements. The first ODS statement defines the location of the PDF file. The second statement closes the file.

In this article, we explain how to use the ODS statement to generate and customize PDF files.

Do you know? How to Create a ZIP File of PDF Files in SAS

Save SAS Output as a PDF File

You can create a PDF file with the SAS Output Delivery System (ODS). The Output Delivery System provides great flexibility for generating files.

You save your SAS output as a PDF file with 3 simple steps:

  1. Create and open a PDF file

    You create a new PDF file with the ODS statement and the PDF keyword. With the FILE=-option you specify the location and the name of the PDF file.

  2. Write SAS code that generates output

    After the ODS statement, you write the SAS code that generates the output that needs to be included in the PDF file.

  3. Close the PDF file

    You close the PDF file with a second ODS statement.

In the example below, we create a PDF file that contains the first 10 rows of the Iris dataset. We save the file in “/folders/myfolders/sas_output/” and call it “iris.pdf”.

/* Save SAS Output as a PDF */
 
/* 1. Open the PDF file and define the location */
ods pdf file="/folders/myfolders/sas_output/iris.pdf";
 
/* 2. Write your SAS code */
title "First 10 Observations of SASHELP.IRIS";
proc print data=sashelp.iris (obs=10) noobs;
run;
 
/* 3. Close the PDF file */
ods pdf close;

As you can see in the code above, you can both save titles and SAS output in a PDF file.

Do you know? How to Select the First N Rows from a Dataset

Save SAS Graph as a PDF File

You can use the Output Delivery System (ODS) also to save a SAS graph as a PDF file. Firstly, you open the PDF file and define its location and name with a first ODS statement. Secondly, you write the SAS code that creates the graph. Finally, you close the PDF file with a second ODS statement.

Below, we create a Pie Chart and save it in a PDF called “iris_pie_chart.pdf”.

ods pdf file="/folders/myfolders/sas_output/iris_pie_chart.pdf";
 
title "Pie Chart of SASHELP.IRIS";
proc sgpie data=sashelp.iris;
    pie species;
run;
 
ods pdf close;
Save a SAS Graph as a PDF file

Do you know? How to Create Beautiful Graphs in SAS

Turn off the Date Time Stamp in a PDF with SAS Output

By default, a PDF generated by SAS contains a date-time stamp at the top right corner of each page. You can remove the date-time stamp from the PDF with the NODATE option. You can combine this option with other options.

In the SAS code below we use the NODATE option to turn the date-time stamp off.

/* Turn off the date time stamp */
options nodate;
 
/* Create the PDF file */
ods pdf file="/folders/myfolders/sas_output/no_datetime_stamp.pdf";
 
title "First 10 Observations of SASHELP.IRIS";
proc print data=sashelp.iris (obs=10) noobs;
run;
 
ods pdf close;
Avoid the date-time stamp in a PDF generated by SAS

Do you know? How to Convert a Date Time Variable into a Date Variable

Turn off the Page Numbers in a PDF with SAS Output

Besides a date-time stamp at the top right corner, SAS also displays the page number. Although page numbers can be useful, you might want to turn them off.

You remove the page numbers in a PDF generated by SAS with the NONUMBER option. You can combine this option with other options, such as NODATE.

Here we remove the page numbers from the PDF file.

/* Turn off Page Numbers */
options nonumber;
 
/* Create the PDF file */
ods pdf file="/folders/myfolders/sas_output/example.pdf";
 
title "First 10 Observations of SASHELP.IRIS";
proc print data=sashelp.iris (obs=10) noobs;
run;
 
ods pdf close;
Don't show page number in a PDF file generated with an ODS statement

Customize Date Time Stamps and Page Numbers

When you use an ODS statement to create a PDF file, SAS automatically adds a date-time stamp and a page number. Above we discussed how to remove them with the NODATE and NONUMBER options. However, it’s also possible to customize them.

You use a footnote statement to customize the date-time stamp and/or page numbers in a PDF generated by SAS. With the footnote statement, you can specify whether you want to show them or not. You can also define their positions.

In the example below, we place a date stamp at the bottom left corner. At the bottom right corner, we show the page number.

/* Customize Date-Time Stamps and Page Numbers */
 
/* 1. Turn off Default Date-Time Stamps and Page Numbers */
options nodate nonumber;
 
/* 2. Define the Escape character */
ods escapechar="^";
 
/* 3. Open the PDF file */
ods pdf file="/folders/myfolders/sas_output/example.pdf";
 
/* 4. Define a footnote with the Date and Page Number*/
footnote j=left "%sysfunc(today(), date9.)"
		j=right "^{thispage} / ^{lastpage}";
 
/* 5. Create SAS Ouput */
title "First 10 Observations of SASHELP.IRIS";
proc print data=sashelp.iris (obs=10) noobs;
run;
 
title "Summary of SASHELP.IRIS by Species";
proc means data=sashelp.iris;
    class species;
run;
 
/* 6. Close the PDF */
ods pdf close;

Do you know? How to Get and Format Today’s Date

Customize Page Breaks in a PDF document

When you create a PDF with the Output Delivery System, then there is (by default) a page break after each SAS procedure. This can cause your PDF to be very long and to have a lot of unused paper. So, how can you avoid page breaks in PDFs generated by SAS?

You need the STARTPAGE=-option, to control page breaks in PDF files that contain SAS output. The STARTPAGE=-option is part of the ODS statement and can be used multiple times while generating a PDF.

The STARTPAGE-option has 5 possible values:

  1. YES: This is the default value. SAS creates a new page after each procedure.
  2. NOW: SAS inserts a new page directly.
  3. NO: SAS doesn’t insert a new page after each procedure. It only creates a page if the page is full or a new page is requested with STARTPAGE=NOW.
  4. NEVER: SAS never inserts a new page. This might graphics to overprint.
  5. BYGROUP: SAS inserts a new page after each BY-group.

In the example below, we use the STARTPAGE=NO option to insert only new pages when the current page is full.

ods pdf file="/folders/myfolders/sas_output/no_page_break.pdf"
startpage=no;
 
title "First 10 Observations of SASHELP.IRIS";
proc print data=sashelp.iris (obs=10) noobs;
run;
 
title "Summary of SASHELP.IRIS by Species";
proc means data=sashelp.iris;
    class species;
run;
 
ods pdf close;

Customize Page Orientation

Besides controlling if and when page breaks occur, you can also control the page orientation. In other words, should the page be in portrait mode or landscape mode?

When you save SAS output as a PDF file, you can change the page orientation with the ORIENTATION=-option. This global option has two values, namely portrait (default) and landscape. A PDF document can contain a mix of portrait and landscape orientated pages.

By default, each page in a PDF generated by SAS is in portrait mode. However, you might need to change the page orientation if your output is very wide but not very long.

In the example below, we create a PDF document of two pages with different orientations. The second page is in landscape mode because it contains a wide SAS table.

/* 1. Open a PDF file */
ods pdf file="/folders/myfolders/sas_output/orientation_combined.pdf";
 
/* 2. Set the page orientation to Portrait */
options orientation=portrait;
 
/* 3. Generate SAS Output */
title "Summary of SASHELP.CARS";
proc means data=sashelp.cars;
run;
 
/* 4. Set the page orientation to Landscape */
options orientation=landscape;
 
/* 5. Generate SAS Output */
title "First 10 Observations of SASHELP.CARS";
proc print data=sashelp.cars (obs=10) noobs;
run;
 
/* 6. Close the PDF file */
ods pdf close;

Turn off Table of Contents

By default, the Output Delivery System (ODS) adds a table of contents to each PDF document. However, you don’t always need this. So, how do you remove a table of contents from a PDF file in SAS?

When you save SAS output as a PDF file, you can turn off the table of contents with the NOTOC=-option. The NOTOC=-option is part of the ODS statement.

In the example below, we use an ODS statement to create a PDF file without the table of contents.

ods pdf file="/folders/myfolders/sas_output/no_toc.pdf"
	notoc;
 
title "First 10 Observations of SASHELP.IRIS";
proc print data=sashelp.iris (obs=10) noobs;
run;
 
title "Summary of SASHELP.IRIS by Species";
proc means data=sashelp.iris;
    class species;
run;
 
ods pdf close;

Customize Table of Contents

By default, the table of contents of a PDF file generated by SAS contains the names of the procedures. For example, “The Print Procedure” or “The Means Procedure”. This isn’t very descriptive. So, how do you control the table of contents when you save SAS output as a PDF file?

You can control the table of contents with the PROCLABEL option. After the PROCLABEL option, you define the name of each section. This option is part of the ODS statement and must be placed before each procedure.

In the example below, we label the PRINT and MEANS statements with the PROCLABEL option.

ods pdf file="/folders/myfolders/sas_output/control_toc.pdf";
 
ods proclabel "First 10 Observations";
title "First 10 Observations of SASHELP.IRIS";
proc print data=sashelp.iris (obs=10) noobs;
run;
 
ods proclabel "Summary";
title "Summary of SASHELP.IRIS by Species";
proc means data=sashelp.iris;
    class species;
run;
 
ods pdf close;

Do you know? How to Create Variable Labels

Control the Style of a PDF File with SAS Output

When you use the default setting, SAS generates a basic, black-and-white PDF file. If you want to change the style of the PDF document, you can use the STYLE=-option in the ODS statement.

In the example below, we show how to apply the STYLE=-option and change the appearance of the SAS output in the PDF file.

ods pdf file="/folders/myfolders/sas_output/set_style.pdf"
    style=FancyPrinter;
 
title "Style: FancyPrinter";
proc print data=sashelp.iris (obs=10) noobs;
run;
 
ods pdf close;
Control the style of the SAS Output in a PDF file: Fancy Printer
Control the style of the SAS Output in a PDF file: Seaside Printer
Control the style of the SAS Output in a PDF file: Monospace
Control the style of the SAS Output in a PDF file: Sapphire

See this list for an overview of the available styles.

2 thoughts on “How to Save SAS Output as a PDF File

Comments are closed.