Pages

Friday, June 22, 2012

How to export SAS results to an Excel spreadsheet?

In order to use the techniques , you must have the following software:
1. Base SAS 9.1.3 or later, on any supported operating system and hardware.
2. Microsoft Excel 2002 or later (also referred to as Microsoft Excel XP).
3. An updated version of the SAS ExcelXP ODS tagset.

We will be using the Output Delivery System (ODS) to do so.  ODS allows you to generate tabular output from your raw output that can be placed into Excel sheets.  In the code below, we are creating an Excel file (giving it a name and location), indicating a style to be used ("minimal" in this example), and specifying a few other options. 
ODS TAGSETS.EXCELXP
file='D:\work\sas9\regression.xls'
STYLE= minimal
OPTIONS ( Orientation = 'landscape'
FitToPage = 'yes'
Pages_FitWidth = '1'
Pages_FitHeight = '100' );
 
ODS TAGSETS.EXCELXP file='D:\work\sas9\tab2.xls' STYLE=Printer OPTIONS ( Orientation = 'landscape' FitToPage = 'yes' Pages_FitWidth = '1' Pages_FitHeight = '100' embedded_titles = 'yes'); 

The first ODS statement ( ) closes the LISTING destination, which writes output to a listing file in batch mode, or to
the Output window when SAS is run interactively. We only want to generate XML output for use with Excel.
The second ODS statement ( ) uses the ExcelXP tagset to generate the XML output and then stores the output in a
file. The STYLE option controls the appearance of the output, such as the font and color scheme. To see a list of
ODS styles that are available for use at your site, submit the following SAS code:
ods listing;
proc template; list styles; run; quit;
The third ODS statement ( ) closes and releases the XML file so that it can be opened with Excel.

We employed a few of the "options" to format our results in Excel.  To see the full list of options, run the SAS code below:
filename temp temp; ods tagsets.ExcelXP file=temp options(doc='help'); ods tagsets.ExcelXP close;

USING ODS TO CREATE THE MULTI-SHEET EXCEL WORKBOOK
By default, the ExcelXP tagset will create a new worksheet each time a SAS procedure creates new tabular output.
Our sample code executes the REPORT procedure to create the first worksheet. Subsequent worksheets are
created by the PRINT procedure, with a new worksheet being created for each distinct BY group.



SAS CODE TO CREATE THE EXCEL WORKBOOK
Here is a listing of the basic SAS code used to create the Excel workbook:

options center;
ods listing close;
ods tagsets.ExcelXP path='output-directory' file='trial.xml' style=XLsansPrinter;
* Create a summary table to use as the summary/table of contents;
proc means data=sample.trial sum noprint;
by patient;
var arrhythmia flutteratr angina;
id sex drug;
output out=trialsummary(drop=_type_ _freq_) sum=;
run; quit;
* Create the summary/table of contents worksheet;
title 'Summary of Adverse Events';
footnote;
proc report nowindows data=trialsummary split='*';
columns patient sex drug arrhythmia flutteratr angina;
define patient / display;
define sex--drug / display;
define arrhythmia--angina / analysis n;
rbreak after / summarize;
run; quit;
* Create the detailed worksheets for each patient;
title 'Patient Visit Log';
footnote 'Click to return to AE Summary';
options missing=' ';
proc print data=sample.trial noobs label split='*';
by patient sex drug dosage;
pageby patient;
id visit visitdate;
var systolic diastolic;
var arrhythmia flutteratr angina;
label patient = 'Patient'
sex = ' Gender'
drug = ' Treatment'
dosage = ' Dosage (mg)';
run; quit;
options missing='.';
ods tagsets.ExcelXP close;

Asone can see ( ), the ExcelXP tagset is used to generate the output, and the XLsansPrinter style is used to
control the appearance aspects of the output. The MEANS procedure ( ) is used to create a summary table, and
PROC REPORT ( ) is run to create the first worksheet based on this summarized data. The PRINT procedure ( )



No comments:

Post a Comment