Page 1
IntroductionSolutions
Conclusions
Maintaining Formats when Exporting Datafrom SAS into Microsoft Excel
Nate Derby & Colleen McGahan
Stakana Analytics, Seattle, WABC Cancer Agency, Vancouver, BC
Regina SAS Users Group3/11/15
Nate Derby & Colleen McGahan Organizing SAS Files 1 / 24
Page 2
IntroductionSolutions
Conclusions
Outline
1 Introduction
2 SolutionsThe ExcelXP TagsetDynamic Data Exchange (DDE)The LIBNAME Engine
3 Conclusions
Nate Derby & Colleen McGahan Organizing SAS Files 2 / 24
Page 3
IntroductionSolutions
Conclusions
Introduction
Many typical ways of exporting data from SAS into Excel destroythe data formats.
Creating Data FormatsDATA 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
Page 4
IntroductionSolutions
Conclusions
SAS Dataset
Nate Derby & Colleen McGahan Organizing SAS Files 4 / 24
Page 5
IntroductionSolutions
Conclusions
Exporting SAS Data
Now let’s export it via PROC EXPORT and the ExcelXP tagset:
SAS CodePROC EXPORT DATA=class
OUTFILE="&outroot\Output from PROC EXPORT.xls";RUN;
ODS tagsets.ExcelXPFILE="&outroot\Output from ExcelXP.xls";
PROC PRINT DATA=class;RUN;
ODS tagsets.ExcelXP CLOSE;
Nate Derby & Colleen McGahan Organizing SAS Files 5 / 24
Page 6
IntroductionSolutions
Conclusions
PROC EXPORT Output
Nate Derby & Colleen McGahan Organizing SAS Files 6 / 24
Page 7
IntroductionSolutions
Conclusions
PROC EXPORT Output
Nate Derby & Colleen McGahan Organizing SAS Files 7 / 24
Page 8
IntroductionSolutions
Conclusions
ExcelXP Tagset Output
Nate Derby & Colleen McGahan Organizing SAS Files 8 / 24
Page 9
IntroductionSolutions
Conclusions
ExcelXP Tagset Output
Nate Derby & Colleen McGahan Organizing SAS Files 9 / 24
Page 10
IntroductionSolutions
Conclusions
SAS Formats vs. Excel Formats
SAS Formats vs. Excel Formats
SAS format Excel format Excel format name$8. @ Text8.2 0.00 Number, 2 decimal placesz8.2 00000.00 (none)
percent8.2 0.00% Percentage, 2 decimal placesmmddyy8. 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
Page 11
IntroductionSolutions
Conclusions
The ExcelXP TagsetDynamic Data Exchange (DDE)The LIBNAME Engine
ExcelXP Tagset Solution
SAS CodeODS 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
Page 12
IntroductionSolutions
Conclusions
The ExcelXP TagsetDynamic Data Exchange (DDE)The LIBNAME Engine
ExcelXP Tagset Solution
Nate Derby & Colleen McGahan Organizing SAS Files 12 / 24
Page 13
IntroductionSolutions
Conclusions
The ExcelXP TagsetDynamic Data Exchange (DDE)The LIBNAME Engine
ExcelXP Tagset Solution with PROC TEMPLATE
SAS CodePROC TEMPLATE;
DEFINE STYLE styles.mystyle;PARENT = styles.default;STYLE data_num from data / TAGATTR='format:0.00';END;
RUN;
ODS tagsets.ExcelXPFILE="&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
Page 14
IntroductionSolutions
Conclusions
The ExcelXP TagsetDynamic Data Exchange (DDE)The LIBNAME Engine
Dealing with Missing Values
SAS CodeOPTIONS MISSING='';
ODS tagsets.ExcelXPFILE="&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
Page 15
IntroductionSolutions
Conclusions
The ExcelXP TagsetDynamic 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
Page 16
IntroductionSolutions
Conclusions
The ExcelXP TagsetDynamic Data Exchange (DDE)The LIBNAME Engine
Dynamic Data Exchange (DDE) Solution
Nate Derby & Colleen McGahan Organizing SAS Files 16 / 24
Page 17
IntroductionSolutions
Conclusions
The ExcelXP TagsetDynamic 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 aheadof time.We then pour the data into the template.
Nate Derby & Colleen McGahan Organizing SAS Files 17 / 24
Page 18
IntroductionSolutions
Conclusions
The ExcelXP TagsetDynamic Data Exchange (DDE)The LIBNAME Engine
The LIBNAME Solution
Nate Derby & Colleen McGahan Organizing SAS Files 18 / 24
Page 19
IntroductionSolutions
Conclusions
The ExcelXP TagsetDynamic Data Exchange (DDE)The LIBNAME Engine
The LIBNAME Solution
Nate Derby & Colleen McGahan Organizing SAS Files 19 / 24
Page 20
IntroductionSolutions
Conclusions
The ExcelXP TagsetDynamic Data Exchange (DDE)The LIBNAME Engine
The LIBNAME Solution
Nate Derby & Colleen McGahan Organizing SAS Files 20 / 24
Page 21
IntroductionSolutions
Conclusions
The ExcelXP TagsetDynamic Data Exchange (DDE)The LIBNAME Engine
The LIBNAME Solution
SAS CodeLIBNAME 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
Page 22
IntroductionSolutions
Conclusions
The ExcelXP TagsetDynamic Data Exchange (DDE)The LIBNAME Engine
The LIBNAME Solution
Nate Derby & Colleen McGahan Organizing SAS Files 22 / 24
Page 23
IntroductionSolutions
Conclusions
Conclusions
Many ways of exporting data from SAS into Excel destroydata 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
Page 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