Page 1 of 16 SAS® Output to Excel, So Many Choices, What Do I Do? Steve Rhoades & Cathryn Ooro, IMS Health Plymouth Meeting, Pa ABSTRACT: SAS gives you a lot of choices for getting your data into Excel and it can get confusing pretty quickly. You can create a variety of files such as CSV, HTML, XML and XLS via SAS using Data _NULL_, ODS or Proc Export. So are there any other choices? Well your other choices within a workbook are a single spreadsheet, multiple worksheets or multiple outputs in a single sheet. And of course there are differences between the mainframe and PC. This presents some challenges when the data is on the mainframe. So what do you need to do in these cases? In our shop there is not much time for learning so we prefer seeing the input, output and the technical details. It tends to speed the development process. For the poster, the flow will be the same to reduce the learning curve. Now you can really get into this by the use of templates which is a topic in itself. However, there will be some references to it here. INTRODUCTION: Your boss calls you in giving you new specs for a project with a very tight deadline, sound familiar? Let’s make it easy, this is an internal report so it does not have to be as pretty as one going to clients. Still it may need to be created on the mainframe or Windows, there are other options but not in this paper. Do we produce one workbook with a single worksheet, or multiple worksheets within a single workbook or multiple outputs in a single worksheet? Other questions, do we need style sheets or post processing and how do we deliver the spreadsheets. We often use email and SAS does have a facility to do that. This paper will have a number of examples to help you quickly see what is needed. Posters NESUG 2009
16
Embed
SAS® Output to Excel, So Many Choices, What Do I Do? · Ex 4: Windows create a single workbook with multiple Excel worksheets and email it. Ex 5: Windows create a single workbook
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1 of 16
SAS® Output to Excel, So Many Choices, What Do I Do?
Steve Rhoades & Cathryn Ooro, IMS Health Plymouth Meeting, Pa
ABSTRACT:
SAS gives you a lot of choices for getting your data into Excel and it can get
confusing pretty quickly. You can create a variety of files such as CSV, HTML,
XML and XLS via SAS using Data _NULL_, ODS or Proc Export. So are there any
other choices? Well your other choices within a workbook are a single spreadsheet,
multiple worksheets or multiple outputs in a single sheet.
And of course there are differences between the mainframe and PC. This presents
some challenges when the data is on the mainframe. So what do you need to do in
these cases?
In our shop there is not much time for learning so we prefer seeing the input, output
and the technical details. It tends to speed the development process. For the poster,
the flow will be the same to reduce the learning curve. Now you can really get into
this by the use of templates which is a topic in itself. However, there will be some
references to it here.
INTRODUCTION:
Your boss calls you in giving you new specs for a project with a very tight deadline,
sound familiar? Let’s make it easy, this is an internal report so it does not have to
be as pretty as one going to clients. Still it may need to be created on the mainframe
or Windows, there are other options but not in this paper. Do we produce one
workbook with a single worksheet, or multiple worksheets within a single workbook
or multiple outputs in a single worksheet? Other questions, do we need style sheets
or post processing and how do we deliver the spreadsheets. We often use email and
SAS does have a facility to do that. This paper will have a number of examples to
help you quickly see what is needed.
PostersNESUG 2009
Page 2 of 16
SYNOPSIS OF EXAMPLES CONTAINED IN THIS PAPER
Ex 1: Mainframe create a single workbook with multiple Excel worksheets and
email it
Ex 2: Mainframe create a single workbook and with multiple Excel (HTML)
Outputs to the same worksheet
Ex 3: Mainframe create multiple workbooks with output like Ex 2, create a second
workbook.
Ex 4: Windows create a single workbook with multiple Excel worksheets and email
it.
Ex 5: Windows create a single workbook and with multiple Excel (HTML) Outputs
to the same worksheet.
Ex 6: Insert leading spaces and percents into an Excel spreadsheet.
EXAMPLE 1 – Mainframe processing to create a Workbook with multiple worksheets.
This example reads in data and creates three different worksheets within a
workbook. It will also show you how to automatically email them
EXAMPLE 1
JCL PIECES NEEDED FOR THE EXCEL FILE The IEFBR14 utility below is creating a file structure to contain the Excel spreadsheets. Note particularly the LIBRARY, RECFM and DSORG statements. //S05A EXEC PGM=IEFBR14
The file created by the IEFBR14 program is used here to write out the Excel files, it was done this way to prevent job errors. I1IN is the main data file and the I4EMAIL file specifies who gets the emailed spreadsheet. //S10A EXEC SASP
Contents of the W05209.xls spreadsheet after opening. Notice the three separate tabs.
EXAMPLE 2 - Mainframe create a single workbook and put Excel Outputs in the same Excel worksheet
In this example two outputs of a Proc Print are written to the same worksheet.
Output is not limited to Proc Print there are a number of choices for output. Before
getting started there are a few points that will be covered. The O1PDSE file will still
need to be created, see Example 1 for how to do this. The ODS code writes output to
an HTML file that is downloaded to a PC directory using FTP BINARY data
transfer. From within Excel the file is opened, fortunately Excel can read HTML
code directly.
PostersNESUG 2009
Page 5 of 16
EXAMPLE 2 JCL Pieces You will need a PDSE file, See Example 1 on how to create
one.
//S10A EXEC SASP
//O1PDSE DD DSN=STS.SJR.NESUG002.W&MMWYY,DISP=SHR
DATA - In this example the data is created using INFILE DATALINES.
PROGRAM (NOTE: This is a partial print, the key part is the Proc Prints are inside the ODS code and it is going to HTML) DATA WklyData WkDate (Keep=WkDate) ;
INFILE Datalines ;
INPUT @1 OUTLETCTR 2.
@3 NS_CHANNEL_TYPE $1.
@4 CMF7 $CHAR07.
@11 FACTOR 4.2
@15 Wkmm $Char02.
@17 Wkw $Char01.
@18 WKyy $CHAR02.
;
WkDate=Compress(WKYY || WKMM || WkW);
AvgPF_Per_CMF7_ChannelWkly = Factor / OutletCtr;
OUTPUT;
Drop WKYY WKMM WkW;
Datalines;
02W1111511442205209
22W1111101942205209
;
ODS LISTING CLOSE; Close Listing Window
ODS HTML Open HTML WINDOW
STYLE=MINIMAL
BODY="WklyData" (URL="WklyData.HTML") Write to HTML
PATH =O1PDSE(URL=NONE)
RECORD_SEPERATOR=NONE
TRANTAB=ASCII;
RUN;
Proc Print data=WklyData (Obs=&PrntObs);
Title "Print of weekly data file";
Run;
Proc Print data=WKDate (Obs=&PrntObs);
Title "Print of week date file ";
Run;
ODS TAGSETS.HTML CLOSE; Close HTML Window
ODS LISTING ; Open HTML Window
PostersNESUG 2009
Page 6 of 16
After the job runs,WklyData file is created
Next download the file
Notice I added the HTML suffix to the file on the PC, then open Excel and read in the file: Contents of the W05209.HTML spreadsheet after opening. Notice the two Prints within a single Worksheet.
PostersNESUG 2009
Page 7 of 16
Two Outputs within a single worksheet.
EXAMPLE 3 - Mainframe putting two Excel Outputs in one worksheet and create a second workbook containing a single worksheet.
This example starts just like Example 2, this time the ODS process uses Proc Print
to write two files to one workbook, closes and then opens a second time to create
another workbook. Output is not limited to Proc Print there are a number of
choices for output. Before getting started there are a few points that will be
covered. The O1PDSE file will still need to be created, see Example 1 for how to do
this. The ODS code writes output to an HTML file that is downloaded to a PC
directory using FTP BINARY data transfer. From within Excel, click Open and
navigate to the folder and file. Highlight the file and double click to open it in Excel.
PostersNESUG 2009
Page 8 of 16
EXAMPLE 3 JCL Pieces You will need a PDSE file, See Example 1 or 2.
DATA - If you can create data for Proc Print, Report, Freq among others, you can use this technique.
PROGRAM (NOTE: The output data files are being used in the Proc Prints within the ODS code) DATA WklyData WkDate (Keep=WkDate)
ANOTHERFILE (KEEP= NS_CHANNEL_TYPE CMF7); Creating a third file
INFILE Datalines ;
INPUT @1 OUTLETCTR 2.
@3 NS_CHANNEL_TYPE $1.
@4 CMF7 $CHAR07.
@11 FACTOR 4.2
@15 Wkmm $Char02.
@17 Wkw $Char01.
@18 WKyy $CHAR02.
;
WkDate=Compress(WKYY || WKMM || WkW);
AvgPF_Per_CMF7_ChannelWkly = Factor / OutletCtr;
OUTPUT;
Drop WKYY WKMM WkW;
Datalines;
02W1111511442205209
22W1111101942205209
;
/* This portion is the same as example 1 other than body names were changed*/ ODS LISTING CLOSE; Close Listing Window
ODS HTML Open HTML WINDOW
STYLE=MINIMAL
BODY="SPRD1"(URL="SPRD1.HTML")Write to HTML
PATH =O1PDSE(URL=NONE)
RECORD_SEPERATOR=NONE
TRANTAB=ASCII;
RUN;
Proc Print data=WklyData (Obs=&PrntObs);
Title "Print of weekly data file";
Run;
Proc Print data=WKDate (Obs=&PrntObs);
Title "Print of week date file ";
Run;
ODS TAGSETS.HTML CLOSE; Close HTML Window
ODS LISTING ; Open HTML Window
ODS HTML CLOSE;
PostersNESUG 2009
Page 9 of 16
Run;
/* This part is new, notice the (2) */
ODS HTML (2) Create another workbook
STYLE=DEFAULT
BODY="BODY2"(URL="SPRD2.HTML")
PATH=O3PDSE(URL=NONE)
RECORD_SEPERATOR=NONE
TRANTAB=ASCII;
Proc Print data=AnotherFile NOOBS;
TITLE 'PRINT OF ANOTHER FILE';
Run;
ODS HTML (2) CLOSE ; Close the 2nd workbook
Run;
For additional workbook creation, keep incrementing ODS HTML.
Example:
ODS HTML (3)
.
.
ODS HTML (3) CLOSE ; Close the 3nd workbook
Run;
After the job runs, two HTML files are created
Next download the files and add an HTML file suffix.
PostersNESUG 2009
Page 10 of 16
Contents of SPRD1.HTML spreadsheet after opening in Excel contains two outputs within a single Worksheet.
Contents of the second workbook created on the mainframe is in Excel
PostersNESUG 2009
Page 11 of 16
EXAMPLE 4 – Windows Processing to create a Workbook with multiple worksheets.
This example reads in data and creates two different worksheets within a workbook.
It will also show you how to automatically email them.