PharmaSUG2010 - Paper CC22 SAS to Excel with ExcelXP Tagset Mahipal Vanam, Kiran Karidi and Sridhar Dodlapati ABSTRACT Creating XML based excel files is a very convenient and powerful feature of SAS 9 or later version. This paper provides a brief overview of the ODS Tagsets.ExcelXP destination to generate XML output that conform to the Microsoft XML Spreadsheet Specifications and can be opened with Microsoft Excel 2002 (also known as Excel XP) or later version. And also, by using ExcelXP tagsets we can create multi-sheet Microsoft Excel workbooks very quickly and easily. INTRODUCTION The ODS ExcelXP Tagset/Destination generates output in Microsoft s SpreadsheetML XML format, and is extremely flexible with many options. It is used specifically for importing data into Excel (2002 or later version). By default, the titles and footnotes in the SAS code will be part of spreadsheet header and footer, i.e., titles and footnotes are only seen either in print preview or when the document is printed. Also by default, the printing orientation will be in 'Portrait', but can be changed to landscape. The tagset options control the appearance and functionality of the Excel workbook. Many of these tagset options are simply tied straight into existing Excel options or features. The code used in this paper should be run in SAS 9.1 or later release, and the generated XML file should be opened with Microsoft Excel 2002 or later version. ExcelXP tagset was experimental in SAS 8.2. This technique doesn t need to have license for SAS/ACCESS to PC files software, as it just needs SAS Base and works on any platform on which SAS software is installed. The ExcelXP tagset offers complete control over worksheets in a workbook, such as Creating multiple worksheets within a workbook very easily. Controlling worksheet name within a workbook. Incorporating titles in the worksheet or in the print preview. Adjusting the column and row heights by using options. By default they are automatically calculated based on the font size and data. Controlling a multitude of printing options for each worksheet, including column and row repeat, scale, and orientation. SYNTAX The syntax needed to generate XML output, which is compatible with versions 2002 and later of Microsoft Excel: ODS Tagsets.ExcelXP file='file-name.xml' style=stylename options(option-name1='value1' option-name2='value2'...); *SAS code which produces output starts here; *SAS code ends here; ODS Tagsets.ExcelXP CLOSE; *must close Tagsets.ExcelXP here*; file='file-name.xml': specifies your external file that contains the XML output. We can just write the file name to save it in a default directory or provide full path including file name to save it in a different directory. style=stylename: Optional. If we don't specify style option, "default" style will be used. 1
options (option-name1='value1' option-name2='value2'...): Optional. If we don't specify anything default options will be used. ODS Tagsets.ExcelXP CLOSE: Statement closes and releases the XML file so that it can be opened with Excel. EXAMPLES Example 1: Basic code with all default settings: ods Tagsets.ExcelXP file='c:\temp\test.xml'; title 'Listing of Class data'; The figure below, generated from above code, shows the default settings of the ExcelXP tagset, including the default style, the default title settings, and the default sheet naming conventions. i.e. the default style is the gray background and the blue column headings. The default title settings place the titles and footnotes in the header and footer, respectively, which will show in print preview or when you print the document. The default sheet name is Table n Data Set dataset name, as shown in below figure. TAGSETS.EXCELXP OPTIONS As mentioned earlier, the ExcelXP tagset supports several options that control both the appearance and functionality of the Excel workbook. Many of these tagset options are simply tied straight into existing Excel options or features. Tagset options are specified on the ODS statement using the "options" keyword: ods Tagsets.ExcelXP options(option-name1='value1' option-name2='value2'...); To see the ExcelXP options/documentation within the SAS log, submit the following SAS code: ods tagsets.excelxp file="test.xml" options(doc="help"); ods tagsets.excelxp close; By submitting the above code, the help text in SAS log shows options including, orientation, frozen_headers, frozen_rowheaders, autofilter, autofilter_table, default_column_widths, sheet_interval, sheet_name, sheet_label, and auto_subtotals, etc... It is important to note that tagset options remain in effect until they are either turned off or set to another value. Since multiple ODS statements are allowed, it is good practice, in terms of functionality and code readability, to explicitly reset tagset options to their default values when you are finished using them. 2
As mentioned earlier, by default, SAS titles and footnotes appear as Excel print headers and print footers, which are displayed only when the Excel document is printed/previewed. To include the titles and footnotes in the worksheets, use the EMBEDDED_TITLES and EMBEDDED_FOOTNOTES options: ods tagsets.excelxp options(embedded_titles='yes' embedded_footnotes='yes'); Example 2: Option set to orientation as landscape: ods Tagsets.ExcelXP file='c:\temp\test1.xml' options(orientation='landscape'); Example 3: Controlling worksheet names: ODS generates a unique name for each worksheet, as required by Microsoft Excel. There are, however, several tagset options that you can use to alter the names of the worksheets. The Sheet_Name option is used to explicitly set the worksheet name. ods Tagsets.ExcelXP file='c:\temp\test2.xml' options(sheet_name='class'); 3
Example 4: Creating multiple worksheets and controlling their names: When you create multiple outputs, each table will be placed in its own worksheet within a workbook. Worksheet names can be up to 31 characters long. This name will be used in combination with a worksheet counter to create a unique name. ods Tagsets.ExcelXP file='c:\temp\test3.xml' options(sheet_name='temp'); proc print data=sashelp.manage; Example 5: Assigning each worksheet a unique name, instead of using one name and counter attached to it for subsequent worksheets: ods Tagsets.ExcelXP file='c:\temp\test4.xml' options(sheet_name='class Data'); proc print data=class; 4
ods tagsets.excelxp options(sheet_name='demog Data'); proc print data=demog; Example 6: Controlling Auto Filter: ods Tagsets.ExcelXP file='temp\test5.xml' options(sheet_name='demog data' AutoFilter='All'); proc print data=demog noobs; var subjid Sex Age Height Weight; Example 7: Using STYLE option: The STYLE option controls the appearance of the output, such as the font and color scheme etc. ods Tagsets.ExcelXP file='temp\test6.xml' STYLE=sansPrinter; proc print data=demog noobs; 5
var subjid Sex Age Height Weight; Although not an ExcelXP option, the ODS option STYLE can be used with the ExcelXP tagset. Styles can be customized or created using PROC TEMPLATE, but SAS has many styles built-in for use. To see the names of available built-in styles, submit the following SAS code: proc template; list styles; quit; TAGSETS Tagsets are kind of templates and are defined using Template Procedure. To get a list of SAS supplied tagsets submit the following code: proc template; list tagsets; quit; To see the source for a tagset definition, specify the two-level name of the tagset within Proc Template. The code will then appear in the SAS log. For example to see source for ExcelXP tagset, submit the following code: proc template ; source tagsets.excelxp; quit; Since the ExcelXP tagset is still evolving, there are some limitations and hence its functionality may be changed in future. It is recommended that the user always download the latest ExcelXP tagset from the SAS ODS MARKUP page. The most up-to-date tagsets are available for download from the ODS Markup Resources site at http://support.sas.com/rnd/base/topics/odsmarkup/ In this paper, we used the ExcelXP Tagset version (SAS 9.1.3, v1.28, 08/29/2005). Quick Reference for the TAGSETS.EXCELXP Tagset can also be found in http://support.sas.com/rnd/base/ods/odsmarkup/excelxp_help.html 6
XML XML is a technology that is designed for managing and sharing structured data in a human-readable text file. XML follows industry-standard guidelines and can be processed by a variety of databases and applications. Using XML, application designers can create their own customized tags, data structures, and schemas. In short, XML greatly eases the definition, transmission, validation, and interpretation of data between databases, applications, and organizations. XML SUPPORT IN EXCEL 2002 OR LATER Microsoft s Excel 2002 (also known as Excel XP) or later version supports importing XML documents as well as saving workbooks as XML files. To save a workbook in XML format, select File Save As, and then choose the "XML Spreadsheet" format. Your entire workbook is saved as a single XML file, which can then be imported into one or more SAS datasets (each worksheet is imported into a separate SAS dataset). XML files which conform to the Microsoft XML Spreadsheet Specification can be opened by Excel 2002 or later version, the ODS ExcelXP tagset creates such files. Thus, to import your SAS output that was generated using the ODS ExcelXP tagset into Excel, just open the XML file with Excel. Excel does not support graphics in XML files. REFERENCES http://support.sas.com/rnd/base/ods/odsmarkup/ http://support.sas.com/rnd/base/ods/odsmarkup/excelxp_help.html http://office.microsoft.com/en-us/excel/ha102063961033.aspx V DelGobbo, "Creating Multi-Sheet Excel Workbooks they Easy Way with SAS". http://www2.sas.com/proceedings/forum2007/120-2007.pdf E Gebhart, "The Beginners Guide to ODS MARKUP: Don't Panic!. http://support.sas.com/rnd/base/ods/odsmarkup/p236-31.pdf CONTACT INFORMATION Your comments and questions are valued and encouraged. Contact the author at: Mahipal Vanam (Mahipal.Vanam@EMDSerono.com, Mahipal.V@gmail.com) Kiran Karidi (Kiran.Karidi@Novartis.com, KaridiKiran@yahoo.com) Sridhar Dodlapati (Sridhar.Dodlapati@frx.com, Sridhardpt@yahoo.com) 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. 7