Introduction
Solutions
Conclusions
Maintaining Formats when Exporting Data
from SAS into Microsoft Excel
Nate Derby & Colleen McGahan
Stakana Analytics, Seattle, WA
BC Cancer Agency, Vancouver, BC
Regina SAS Users Group
3/11/15
Nate Derby & Colleen McGahan Organizing SAS Files 1 / 24
Introduction
Solutions
Conclusions
Outline
1
Introduction
2
Solutions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
3
Conclusions
Nate Derby & Colleen McGahan Organizing SAS Files 2 / 24
Introduction
Solutions
Conclusions
Introduction
Many typical ways of exporting data from SAS into Excel destroy
the data formats.
Creating Data Formats
DATA class;
SET sashelp.class;
FORMAT age 3. height weight 6.2;
IF name = 'Thomas' THEN age = .;
RUN;
Nate Derby & Colleen McGahan Organizing SAS Files 3 / 24
Introduction
Solutions
Conclusions
SAS Dataset
Nate Derby & Colleen McGahan Organizing SAS Files 4 / 24
Introduction
Solutions
Conclusions
Exporting SAS Data
Now let’s export it via PROC EXPORT and the ExcelXP tagset:
SAS Code
PROC EXPORT DATA=class
OUTFILE="&outroot\Output from PROC EXPORT.xls";
RUN;
ODS tagsets.ExcelXP
FILE="&outroot\Output from ExcelXP.xls";
PROC PRINT DATA=class;
RUN;
ODS tagsets.ExcelXP CLOSE;
Nate Derby & Colleen McGahan Organizing SAS Files 5 / 24
Introduction
Solutions
Conclusions
PROC EXPORT Output
Nate Derby & Colleen McGahan Organizing SAS Files 6 / 24
Introduction
Solutions
Conclusions
PROC EXPORT Output
Nate Derby & Colleen McGahan Organizing SAS Files 7 / 24
Introduction
Solutions
Conclusions
ExcelXP Tagset Output
Nate Derby & Colleen McGahan Organizing SAS Files 8 / 24
Introduction
Solutions
Conclusions
ExcelXP Tagset Output
Nate Derby & Colleen McGahan Organizing SAS Files 9 / 24
Introduction
Solutions
Conclusions
SAS Formats vs. Excel Formats
SAS Formats vs. Excel Formats
SAS format Excel format Excel format name
$8. @ Text
8.2 0.00 Number, 2 decimal places
z8.2 00000.00 (none)
percent8.2 0.00% Percentage, 2 decimal places
mmddyy8. mm/dd/yy Date, type “03/14/01”
comma12.2 #,##0.00 Number, 2 decimal places, with ...
We need to translate SAS formats into Excel formats!
Nate Derby & Colleen McGahan Organizing SAS Files 10 / 24
Introduction
Solutions
Conclusions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
ExcelXP Tagset Solution
SAS Code
ODS tagsets.ExcelXP
FILE="&outroot\Output from ExcelXP, Numeric Formatting.xls";
PROC PRINT DATA=class;
VAR name sex age;
VAR height weight / STYLE={TAGATTR='format=0.00'};
RUN;
ODS tagsets.ExcelXP CLOSE;
Nate Derby & Colleen McGahan Organizing SAS Files 11 / 24
Introduction
Solutions
Conclusions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
ExcelXP Tagset Solution
Nate Derby & Colleen McGahan Organizing SAS Files 12 / 24
Introduction
Solutions
Conclusions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
ExcelXP Tagset Solution with PROC TEMPLATE
SAS Code
PROC TEMPLATE;
DEFINE STYLE styles.mystyle;
PARENT = styles.default;
STYLE data_num from data / TAGATTR='format:0.00';
END;
RUN;
ODS tagsets.ExcelXP
FILE="&outroot\Output from ExcelXP, Numeric Formatting.xls";
PROC PRINT DATA=class;
VAR name sex age;
VAR height weight / STYLE( data )=data_num;
RUN;
ODS tagsets.ExcelXP CLOSE;
Nate Derby & Colleen McGahan Organizing SAS Files 13 / 24
Introduction
Solutions
Conclusions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
Dealing with Missing Values
SAS Code
OPTIONS MISSING='';
ODS tagsets.ExcelXP
FILE="&outroot\Output from ExcelXP, Numeric Formatting.xls";
PROC PRINT DATA=class;
VAR name sex age;
VAR height weight / STYLE( data )=data_num;
RUN;
ODS tagsets.ExcelXP CLOSE;
OPTIONS MISSING='.';
Nate Derby & Colleen McGahan Organizing SAS Files 14 / 24
Introduction
Solutions
Conclusions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
Dynamic Data Exchange (DDE) Solution
DDE = SAS opens Excel, tells it what to do.
You have to tell Excel every single step.
Best solution: The %exportToXL macro (free!).
SAS Code
%LET exroot = c:\...\exportToXL;
OPTIONS SASAUTOS=( "&exroot" ) MAUTOSOURCE;
%exportToXL( DSIN=class, SAVEPATH=&outroot,
SAVENAME=Output from DDE );
Nate Derby & Colleen McGahan Organizing SAS Files 15 / 24
Introduction
Solutions
Conclusions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
Dynamic Data Exchange (DDE) Solution
Nate Derby & Colleen McGahan Organizing SAS Files 16 / 24
Introduction
Solutions
Conclusions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
The LIBNAME Solution
Requires the SAS/ACCESS for PC Files package.
We “cheat” by (manually) formatting the Excel template ahead
of time.
We then pour the data into the template.
Nate Derby & Colleen McGahan Organizing SAS Files 17 / 24
Introduction
Solutions
Conclusions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
The LIBNAME Solution
Nate Derby & Colleen McGahan Organizing SAS Files 18 / 24
Introduction
Solutions
Conclusions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
The LIBNAME Solution
Nate Derby & Colleen McGahan Organizing SAS Files 19 / 24
Introduction
Solutions
Conclusions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
The LIBNAME Solution
Nate Derby & Colleen McGahan Organizing SAS Files 20 / 24
Introduction
Solutions
Conclusions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
The LIBNAME Solution
SAS Code
LIBNAME workbook PCFILES
PATH="&outroot\Output from LIBNAME.xls";
PROC DATASETS LIBRARY=workbook NOLIST;
DELETE MyRange;
QUIT;
DATA workbook.MyRange;
SET class;
RUN;
LIBNAME workbook CLEAR;
Nate Derby & Colleen McGahan Organizing SAS Files 21 / 24
Introduction
Solutions
Conclusions
The ExcelXP Tagset
Dynamic Data Exchange (DDE)
The LIBNAME Engine
The LIBNAME Solution
Nate Derby & Colleen McGahan Organizing SAS Files 22 / 24
Introduction
Solutions
Conclusions
Conclusions
Many ways of exporting data from SAS into Excel destroy
data formats.
SAS and Excel speak different languages for data formats.
This can be fixed in three ways:
ExcelXP Tagset with the TAGATTR style.
Dynamic Data Exchange with %exportToXL macro.
The LIBNAME engine with pre-formatted template.
Nate Derby & Colleen McGahan Organizing SAS Files 23 / 24
Appendix
Further Resources
Too many to list – see the paper!
Nate Derby: [email protected]
Colleen McGahan: [email protected]
Nate Derby & Colleen McGahan Organizing SAS Files 24 / 24