Tag It, Bag It, Put It Out into Excel

Tag It, Bag It, Put It Out into Excel
Ethan Miller, SRI International, Menlo Park, CA
ABSTRACT
So you have to put data out from SAS® to Excel, again. You don’t want the clients to ignore your output like they did
last time. To make your Excel output command attention, PROC REPORT and TAGSETS.MSOFFICE2K are your
weapons of choice. This paper will illustrate how to create hyperlinks, highlight rows, and conditionally shade cells
(traffic lighting). This technique is an easy way to put out data in Excel for people who do not use SAS. This paper
was written for those with beginning skills; the code was written using SAS version 9.2 on Windows OS.
INTRODUCTION
Sometimes data cleaning decisions are most efficiently made by someone other than a programmer (such as a
research assistant or by the clients who collected the data). The code for this paper was developed in working on a
data cleaning task that involved creating multiple Excel workbooks that each went to a different agency. The Excel
files contained data issues that had to be resolved before any analysis could be run. Staff from each agency made
corrections to their data via the Excel workbooks we provided, based on their databases or from images of
questionnaires they had completed. These Excel files were then brought back into SAS and data were recoded. The
code presented here generates the Excel files containing the data issues to be resolved. To protect the identities of
the innocent, a bogus dataset was created to show the techniques presented in this paper.
GETTING STARTED
To create a dataset for this paper, I started with the CLASS dataset from SASHELP and added a few variables to
make it interesting. I put out the following table to Excel using PROC REPORT and TAGSETS.MSOFFICE2K.
1
This code was used to generate the above table:
ods tagsets.excelxp file="Path\WUSS1.xml" style=minimal
options(
autofilter='ALL'
frozen_headers='yes'
sheet_interval='bygroup'
sheet_label='Table of '
suppress_bylines='yes'
);
proc report data=class_2 nowd split = '^’ style(header)={font_weight=bold};
column Name name_db Age Height Sex Weight nameflag quesimage score ;
by sex;
define Name /display ;
define name_db /display "Database^Name";
define Age /display ;
define Height /display;
define Sex /display ;
define Weight /display ;
define nameflag /display noprint ;
define quesimage /display ;
define score /display
"Score";
run;
ods tagsets.excelxp close;
HOW IT WORKS
 ODS TAGSETS.EXCELXP is the statement that starts the creation of the Excel file. The ODS
TAGSETS.EXCELXP line is where style attributes are applied. In this case, it is STYLE=MINIMAL. It is also
where various options are specified. For a comprehensive list of these options visit:
http://support.sas.com/rnd/base/ods/odsmarkup/excelxp_help.html. You have to include a statement to close the
file: ODS TAGSETS.EXCELXP CLOSE. Just like other ODS output statements, you have to have both the open
and the close.

The AUTOFILTER option adds a filter to the header row of the Excel file. Here ALL is specified (which means
that every column with data in the worksheet has a filter), but you could also list a column number or a range of
columns.

FROZEN_HEADERS=yes freezes the header row in Excel.

The SHEET_INTERVAL option controls the creation of worksheets. By default a new worksheet is created for
each PROC. Here we specify BYGROUP, which creates one worksheet for each by variable combination. If we
specified NEVER, there would be a single worksheet.

SHEET_LABEL controls the prefix of the worksheet name. If SHEET_LABEL is set to “”, no prefix will be
displayed, and the worksheets will be named by the values of your BY variable. Here we use the prefix “Table of
“ as a prefix for our worksheet names.

SUPPRESS_BYLINES = yes suppresses the “BY variable =” line from being added to the header row of the
worksheet. This allows the variable names to show up in row 1.

A SPLIT character is defined as a “^”. The “^” will be used to add breaks to our header row. For example, we
use it to add a line break after database for the variable name_db.

STYLE attributes are controlled on the PROC line. Here we are bolding our header row in the worksheet.
2
ADDING FEATURES
The following is an example of the final Excel workbook that was created using PROC REPORT and
TAGSETS.MSOFFICE2K. The workbook contains two worksheets, “Table of F” and “Table of M”, one for men and
one for women. Code was used to add traffic lighting, hyperlinks, and shading.
This syntax generates the above figure. Code that is shaded was used to generate the first figure. Code that is not
shaded is new for this example.
ods tagsets.excelxp file="Path\WUSS2.xml" style=minimal
options(
autofilter='ALL'
frozen_headers='yes'
sheet_interval='bygroup'
sheet_label='Table of '
suppress_bylines='yes'
);
proc report data=class_2 nowd split = '^’ style(header)={font_weight=bold};
column Name name_db Age Height Sex Weight nameflag quesimage score ;
by sex;
define Name /display ;
define define name_db/display "Database^Name"
style(header)={flyover='Childs Name in Database'};
define Age /display;
define Height /display;
define Sex /display;
define Weight /display;
define nameflag /display noprint;
define quesimage /display style(column) = {foreground=blue
cellwidth=120}
*Traffic lights where name does not match;
define score /display
"Score";
compute nameflag;
if (nameflag = 1) then do;
call define ('name', 'style', 'style=[background=red]');
call define ('name_db', 'style', 'style=[background=red]');
end;
endcomp;
*Hypertext links ;
3
COMPUTE quesimage;
urlstring = "Path\"||TRIM(quesimage);
call define(_col_, 'URL', urlstring);
ENDCOMP;
*Highlight every other row;
compute name;
*Placeholder for row highlighting;
Row+1;
if (mod(Row,2)ne 0)
then call define(_row_,'style','style=[background=silver]');
endcomp;
run;
ods tagsets.excelxp close;
HOW IT WORKS

A flyover is added to the header row. This allows the variable name to be the column header which makes the
data easy to read back into SAS, while making the variable label easily accessible.

Traffic lighting is added using a CALL DEFINE statement within a COMPUTE BLOCK. When the “nameflag”
variable is equal to one, two CALL DEINFE statements are run. In this example, each CALL DEFINE statement
will over ride the current color formatting on the cell, turning them red. In the CALL DEFINE statement, first
comes the variable (or row) of interest. Next, the attribute description is listed, and finally the value. For a
comprehensive list of attribute descriptions visit:
http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002473624.htm

A hyperlink is created using a CALL DEFINE with in a COMPUTE BLOCK. The variable that contains the name
of the questionnaire image (quesimage) is COMPUTED. A variable called “urlstring” is created and it contains the
path to the image with the filename concatenated onto the end of it. Next, CALL DEFINE is used to create a
hyperlink.

The color of the hyperlink is made blue by assigning a style attribute on the define line for the “quesimage”
variable

Every other row is made silver using a COMPUTE BLOCK. A variable for the rows in the report is created.
When this variable is divided by two and a whole number is the result, the style attribute for the row is set to
silver.
ADDITIONAL THOUGHTS
Users need to use version Excel 2002 or later.
Once changes are made to the Excel files (really an .XML file), they can be saved as .XLS files and read back into
SAS.
4
CONCLUSION
For reports that have to be generated in Excel on a regular basis, using PROC REPORT and
TAGSETS.MSOFFICE2K is a great way to have a lot of control over your output. You can create formatting and
functions in SAS rather than creating (and re-creating) the same features each time you create an Excel file.
REFERENCES
DelGobbo, Vincent, 2011. “Creating Styleish Multi-Sheet Microsoft Excel Workbooks the Easy Way with SAS”,SAS
Institute Inc., Cary NC,USA
Gebhart, Eric, (2009), “ODS Packages: Putting some Zip in Your ODS Output”, SAS Institute Inc., Cary,NC,USA.
Mcmahill, Allison. (2007), “Beyond the Basics: Advanced PROC REPORT Tips and Tricks”, SAS Institute Inc.,
Cary,NC,USA.
Pinet, Dominique. (2007), “Using tagsets MSOFFICE2K to customize your Excel report”, KEYRUS Biopharama,
Paris,France.
ACKNOWLEDGMENTS
Thanks to Patrick “Pditty” Thornton, Cyndi Williamson, Kathy Valdes, and my partner in crime Mary McCracken.
RECOMMENDED READING
DelGobbo, Vincent. 2010. "An Introduction to Creating Multi-Sheet Microsoft Excel Workbooks the Easy Way with
SAS®". Available at http://www.sas.com/reg/gen/corp/867226?page=Resources.
DelGobbo, Vincent. 2010. "Vince DelGobbo's ExcelXP Tagset Paper Index".
Available at http://www.sas.com/events/cm/867226/ExcelXPPaperIndex.pdf.
CONTACT INFORMATION
Your comments and questions are valued and encouraged. Contact the author at:
Ethan Miller
Sr. Programmer/Analyst
SRI International
333 Ravenswood Ave BS159
Menlo Park, Ca 94025
Work Phone: (650) 859-5726
E-mail: [email protected]
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS
Institute Inc. in the USA and other countries. ® indicates USA registration.
Other brand and product names are trademarks of their respective companies.
5