Automating Excel workbooks creation using SAS

Related Post: Splitting a data table into multiple sheets of an Excel workbook

No matter which powerful analytical tools data professionals use for their data processing, MS Excel remains the output of choice for many users and whole industries.

In banking and finance, for example, I have seen many SAS users create quite sophisticated data queries and data analysis projects in SAS Enterprise Guide. Yet, at the end, when the final datasets have been produced and validated, comes the manual part when users export those tables into Excel, and then combine and rearrange them by copying-pasting into a desired workbook for storing and distributing.

However, this heavily manual process can be not just fully automated, but also enhanced compared with the point-and-click “export to Excel” and “copy-paste” interactive process. Here is how.

Creating a single simple Excel sheet

Suppose, we want to convert SASHELP.CLASS data table to Excel. Here is a bare-bone solution using SAS Output Delivery System:

ods excel file='C:\Projects\SAS_to_Excel\Single_sheet.xlsx';

proc print data=SASHELP.CLASS noobs;
run;

ods excel close;

This code is pretty much self-explanatory. It will produce Single_sheet.xlsx Excel workbook file in the folder C:\Projects\SAS_to_Excel. When opened in Excel, it will look as follows:

If you browse through the ODS EXCEL documentation you will find a variety of options that allow you to customize Excel output. Let’s get a little fancy by utilizing some of them.

Creating several customized sheets in Excel workbook

The following code example creates two sheets (tabs) in a single workbook. In addition, it demonstrates some other features to enhance data visualization.

/* -------------------------------------------- */
/* Two sheets workbook with enhanced appearance */
/* -------------------------------------------- */

/* Formats for background & foreground coloring */
proc format;
value hbg
50 <- 60 = #66FF99
60 <- 70 = #FFFF99
70 <- high = #FF6666
;
value hfg
low -< 50, 70 <- high = white;
run;

/* Define custom font style for ODS TEXT */
proc template;
define style styles.MyStyle;
parent=styles.htmlblue;
style usertext from usertext /
foreground = #FF33CC
font_weight = bold
;
end;
run;

/* ODS Excel output file destination */
ods excel file = 'C:\Projects\SAS_to_Excel\Two_sheets_fancy.xlsx';

/* Excel options for 1st sheet (tab) */
ods excel options
( sheet_name = 'SASHELP.CLASS'
frozen_headers = 'on'
embedded_titles = 'on' )
style = styles.MyStyle;

title justify=left color='#4D7EBF' 'This is TITLE for SASHELP.CLASS';

ods text='This is TEXT for SASHELP.CLASS';

proc print data=SASHELP.CLASS noobs;
var NAME;
var SEX AGE / style = {just=C};
var HEIGHT / style = {background=hbg. foreground=hfg.};
var WEIGHT;
run;

/* Excel options for 2nd sheet (tab) */
ods excel options
( sheet_name = 'SASHELP.CARS'
frozen_headers = 'on'
embedded_titles = 'on' );

title 'This is TITLE for SASHELP.CARS';

proc print data=SASHELP.CARS noobs;
run;

ods excel close;

Here are the code highlights:

PROC FORMAT creates two formats, HBG and HFG, which are used in the first PROC PRINT to illustrate cell text and background coloring.

NOTE: In SAS, colors are specified as hexadecimal RGB values (cxRRGGBB). However, I found (although it seems undocumented) that in PROC FORMAT and PROC TEMPLATE these colors can be written as quoted (double or single) or unquoted values, as well as prefixed with either ‘#’ or ‘cx’ (for hexadecimal). For example, the following are all valid values: #FFAADD, '#FFAADD', cxFFAADD, 'cxFFAADD'.

PROC TEMPLATE defines usertext custom font style for ODS TEXT, which used in ODS EXCEL along with and as alternative to the TITLE statement.

ODS EXCEL FILE=file-specification statement opens the EXCEL destination and specifies the output file for ODS EXCEL. The output file-specification can be either a physical file name (must be in quotes) or a fileref (without quotes) assigned with FILENAME statement. It can point to a location on the machine where SAS is run (SAS server), or a network drive accessible from the SAS server.

This statement follows by two separate ODS EXCEL OPTIONS statements – one per corresponding sheet (tab).

ODS EXCEL OPTIONS statement specifies destination-specific suboptions with space-delimited name='value' pairs. In particular, we use the following options:

SHEET_NAME= specifies the name for the next worksheet (worksheet names can be up to 28 characters long).

FROZEN_HEADERS='ON' specifies that headers are not scrolled when table is vertically scrolled (default is OFF). It is very convenient feature that keeps the title(s) and column names in view while user scrolls through the data rows.

EMBEDDED_TITLES='ON' specifies whether titles should appear in the worksheet (default is OFF).

There is a variety of other useful ODS EXCEL options allowing further customization of your Excel workbook appearance and functionality.

TITLE statement is highly customizable with Output Delivery System as shown in this custom TITLE with ODS example.

ODS TEXT= statement inserts text into your ODS output. Unlike TITLE for which ODS EXCEL merges several cells, ODS TEXT places its text in a single cell (see screenshot below). The UserText style element that we modified using PROC TEMPLATE controls the font style, font color, and other attributes of the text that the ODS TEXT= statement produces.

In PROC PRINT, we use multiple VAR statements to select variables, determine their order and apply styles (text and background colors) to the printed values. There are much more ODS styles with PROC PRINT available for further customizations.

The next section of the code ( /* Excel options for 2nd sheet (tab) */ ) creates the second sheet in the same Excel workbook. Similarly, you can create as many sheets/tabs as you wish.

The last statement ods excel close; closes the ODS Excel destination so nothing more is written to the output file.

The following are the two screenshots illustrating the two sheets in the produced Excel workbook:

Questions

Do you find this post useful? Do you have questions, comments, suggestions, other tips or tricks about creating MS Excel workbooks in SAS? Please share with us below.

Additional Resources

Exchanging Data From SAS® to Excel: The ODS Excel Destination (book by William E. Benjamin Jr., 2017)

Insights from a SAS Technical Support Guy: A Deep Dive into the SAS® ODS Excel Destination (SESUG paper by Chevell Parker, 2018)

Power Up Your Reporting Using the SAS® Output Delivery System (SGF paper by Chevell Parker, 2019)

Using SAS® ODS EXCEL Destination "Print Features" to Format Your Excel Worksheets for Printing as You Create Them (PharmaSUG paper by William E Benjamin Jr., 2019)

TUNE IN NOW | LEARN HOW TO READ AND WRITE EXCEL FILES WITH SAS
Automating Excel workbooks creation using SAS was published on SAS Users.