SAS Code to Export and Create Pivot Tables in Excel 2007 By Robert Williams Senior Supply Chain Analyst Strategic Sourcing and Contracts The New York Times.

Slides:



Advertisements
Similar presentations
Macro Variables Make Life Easier: Applications with SAS® to Excel Misty Johnson Research Analyst-Senior State of Wisconsin Department of Health Services.
Advertisements

Information Systems Technology Ross Malaga B Copyright © 2005 Prentice Hall, Inc. B-1 WORKING WITH DATABASES.
© 2014 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any manner.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module D (Office 2007 Version) Decision Analysis.
Slide 1 FastFacts Feature Presentation August 28, 2008 We are using audio during this session, so please dial in to our conference line… Phone number:
Slide 1 FastFacts Feature Presentation February 17, 2011 We are using audio during this session, so please dial in to our conference line… Phone number:
User Friendly Price Book Maintenance A Family of Enhancements For iSeries 400 DMAS from Copyright I/O International, 2006, 2007, 2008, 2010 Skip Intro.
Chapter 10 Excel: Data Handling or What do we do with all that data?
Use of Microsoft Office 2003 Excel Pivot Tables ASQ Milwaukee Pre-Meeting Clinic Monday, May 21, 2012 Presented by: Jeff Stumpe.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Intro to Access 2007 Lindsey Brewer CSSCR September 18, 2009.
Benchmark Series Microsoft Excel 2013 Level 2
National Center for Health Statistics Data Online Query System Overview
DATA ANALYTICS. NORMS Cell Phones on Vibrate Respect all opinions.
Excel Lesson 17 Importing and Exporting Data Microsoft Office 2010 Advanced Cable / Morrison 1.
© Paradigm Publishing, Inc Access 2010 Level 2 Unit 2Advanced Reports, Access Tools, and Customizing Access Chapter 8Integrating Access Data.
Creating New Financial Statements In Excel Presented by: Nancy Ross.
Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel.
Jeopardy Objects Navigation Buttons True/False Parts of a Report Vocabulary Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final.
Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill/Irwin TECHNOLOGY PLUG-IN T3 PROBLEM SOLVING USING EXCEL.
BUSINESS DRIVEN TECHNOLOGY Decision Analysis Tools in Excel
McGraw-Hill/Irwin ©2008 The McGraw-Hill Companies, All Rights Reserved TECHNOLOGY PLUG-IN T4 PROBLEM SOLVING USING EXCEL Goal Seek, Solver & Pivot Tables.
Visual Basic for Applications. What it does Extends the features and built in functions of Excel – Create and run VB procedures – Some may be easy to.
Using Basic FormulasUsing Basic Formulas Lesson 4 © 2014, John Wiley & Sons, Inc.Microsoft Official Academic Course, Microsoft Word Microsoft Excel.
5/1/ Quick and Dirty Excel® Workbooks Without DDE or ODS Andrea Zimmerman, Capital One, Richmond, VA.
Lecture 3 Advanced Excel. OVERVIEW Reviewing and Sharing Workbooks Templates Charts and Graphs Sparklines Conditional Formatting Pivot Tables What-If.
Further Adventures in Excel Adding those “Final Touches”…
Project Information: Customizing and Printing Lesson 7.
Working Smarter, Not Harder with DDE: Did I Really Spend All That Time Just Formatting My Report? Bobby Kidd Vice President Senior Statistical Portfolio.
Creating Reports Using Excel Pivot Tables Steve Honeycutt UNCG.
Integrating Access with the Web and with Other Programs.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Let SAS Do the Coding for You! Robert Williams Business Info Analyst Sr. WellPoint Inc.
Chapter 9 Macros, Navigation Forms, PivotTables, and PivotCharts
October 2003Bent Thomsen - FIT 3-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
An Animated Guide©: Sending SAS files to Excel Concentrating on a D.D.E. Macro.
1 Data List Spreadsheets or simple databases - a different use of Spreadsheets Bent Thomsen.
Lesson 17 Getting Started with Access Essentials
Colleague, Excel & Word Best of Friends Presented by: Joan Kaun & Yvonne Nelson College of the Rockies.
Using Visual Basic for Applications in Microsoft Project Sean Vogel.
EXAM REVIEW PROJECT Microsoft Excel Exam 1. EXAM PROCEDURES 10 minutes to review project before starting 60 minutes to complete the exam In this presentation,
Chapter 21: Working with Large Data Re-Visited Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Working with Data Lists.
Gold – Crystal Reports Introductory Course Cortex User Group Meeting New Orleans – 2011.
Selecting Funds and Fund Centers When entering criteria for fields in the BI report variable screen you often don’t know what Fund or Funds Center your.
Printing Reports. Creating Reports  Reports are the best way to put information from database onto paper, PDF files, and other formats.  In a report,
Excel Class Outline What is a spreadsheet? What can you do with them? The Cell - basic unit of a spreadsheet Making a Table - cells in Rows and Columns.
Use SAS to Automate Hospital Reports generation Cardiac Service BC Stats Team, PHSA Tina Yang.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 14 & 19 By Tasha Chapman, Oregon Health Authority.
SAS ® Global Forum 2014 March Washington, DC Arthur Tabachneck Thornhill, ON Canada Tom Abernathy New York, NY Matt Kastin Penn Valley, PA.
Build your Metadata with PROC CONTENTS and ODS OUTPUT Louise S. Hadden Abt Associates Inc.
Microsoft Excel Consolidation. Contents Introduction to Multiple Workbook Applications Working with Multiple Workbook Applications using normal keyboard.
HCAI Information for ACtion 2010
Hudson Fare Files 103 – Alternate Fare Files
Creating a Windchill Report Interface in Excel
PROJECT ON MS-EXCEL.
Excel: Macros & Pivot Tables
Analyzing Data Module 4.
Using ODS Excel Migrating from DDE to ODS
Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts
Excel for EMIS A few of my favorite things
Excel VBA Day 3 of 3 Tom Vorves.
Managing Multiple Worksheets and Workbooks
Microsoft Office Access 2003
Microsoft Office Access 2003
Automating SAS through the Power of VB Script
DATA MANIPULATION Wendy Harrison Mari Morgan Dafydd Williams
Bent Thomsen Institut for Datalogi Aalborg Universitet
Creating a Windchill Report Interface in Excel
Workshop on Excel Dr P Sravan Kumar MBA PhD
Presentation transcript:

SAS Code to Export and Create Pivot Tables in Excel 2007 By Robert Williams Senior Supply Chain Analyst Strategic Sourcing and Contracts The New York Times Company – Shared Service Center

Overview Why need pivot tables in Excel 2007 Excel Step: Excel macro (VB) for pivot table SAS Steps: SAS code to do these steps Step 1:Export data into Excel Step 2:Open Excel file with the VB macro Step 3:Open Excel file with the exported data Step 4:Invoke VB macro to create pivot table Step 5:Save the Excel file with newly created pivot table Q&A

Why Pivot Tables in Excel 2007 Drill down capabilities User friendly hands-on reports Management and decision makers are asking for it. With SAS, the pivot tables can be repeated for production reports on a daily, weekly or monthly basis. Also, the pivot tables can be performed on multiple subsets of data i.e. by business units, departments and etc.

Excel Step: VB macro to pivot First, we need to create a Excel macro that does a pivot table on the data. Easiest way is to use the Excels macro recorder (under Developer menu). Use macro recorder on a sample data to establish the pivot table structure. Name the macro i.e. Create_pivottable Expand the size data range inside the VB code. Save the Excel file with the VB macro as a blank file (must be.xlsm)

Excel Step: VB macro to pivot What does VB code look like? Note the range name and size…

SAS Step 1: Export the data Export the actual data into an Excel 2007 file. The data must have the same column names and sheet name for the VB macro.

SAS Step 2: Open Excel file with the VB macro First, we need to open the Excel file that has the VB macro we created (the blank file). It is opened using SAS x command with the path and file name of the.xlsm file.

SAS Step 3: Open Excel file with the exported data It is important to open this file after opening the VB macro file. That way, the Excel file with the actual data becomes the active window. Before doing this, we have to set up the DDE in SAS. DDE stands for Direct Data Exchange. It allows SAS to control Excel. Once the DDE is set up, we can invoke the VB macro on the exported data.

SAS Step 3: Open Excel file with the exported data SAS code for DDE set up and opening the Excel file with the exported data.

SAS Step 4: Invoke the VB macro to create pivot table To invoke the VB or run other Excel commands, I use the SAS put statement in the data _null_ i.e. put "[run(""'Main Wireless Macro for SAS.xlsm'!Create_wireless_pivottable"")]"; The data _null_ is linked to the DDE under the file statement. Note: I put the sleep statement to allow Excel time to fully open the Excel files before the next command.

SAS Step 5: Save the Excel file with the newly created pivot table At this point, we just created pivot table of the exported data. We save it while still in the data _null_ step using the DDE with this SAS line put '[file.close(true)]'; It closes the active Excel file while setting the Excel saved option as true.

SAS Step 5: Save the Excel file with the newly created pivot table At this point, we just created pivot table of the exported data. We save it while still in the data _null_ step using the DDE with this SAS line put '[file.close(true)]'; It closes the active Excel file while setting the Excel saved option as true. Excel file with the VB macro is still open. Close Excel using put "[quit()]"; Dont forget the sleep before doing this.

Q&A Questions? Contact: Robert Williams The New York Times Company Shared Service Center 101 West Main Street, Suite 2000 Norfolk, VA

References Linking SAS and Microsoft Products by Nat Wooding, Dominion Virginia Power. Presented at Virginia SAS Users Group on June 17, Step-by-Step in Using SAS DDE to Create an Excel Graph Based on N Observations from a SAS Data Set by Choon-Chern Lim, Mayo Clinic, Rochester, MN. Presented at SUGI 31 SAS Online Doc Version 9.2. Cary, NC: SAS Institute Inc. Excel 2007 Online Help Microsoft Corp.

SAS Code /* This is to export the wireless data and invoke */ /* the VB macro to create the pivot table on the */ /* exported data. */ %let xlsdir = H:\SAS Papers\VASUG Spring 2010 Presentation; libname VASUG "&xlsdir"; /* Export the data into Excel prior to doing the pivot table */ PROC EXPORT DATA=VASUG.sample_wireless OUTFILE= "&xlsdir.\VASUG Wireless.xlsx" DBMS=EXCEL LABEL REPLACE; SHEET="Data"; run; /* Opens the Excel macro and establish the DDE to run the Excel VB macro */ %let xlsMacro = &xlsdir\Main Wireless Macro for SAS.xlsm; options noxsync noxwait; x "'&xlsMacro.'"; /* Opens the Excel macro file first */ data _null_; rc = sleep(2); run; filename sas2xl dde 'excel|system'; /* This data step opens the file with the data and runs */ /* the Excel macro to create the pivot table of the data */ data _null_; file sas2xl; put "[open(""&xlsdir.\VASUG Wireless.xlsx"", 0, false)]"; rc = sleep(2); put "[run(""'Main Wireless Macro for SAS.xlsm'!Create_wireless_pivottable"")]"; put '[file.close(true)]'; run; /* Closes the Excel application */ data _null_; rc = sleep(5); run; data _null_; file sas2xl; put "[quit()]"; run;

Excel VB Code Sub Create_wireless_pivottable() ' Creates a pivot table of all the phone data Sheets.Add After:=Sheets(Sheets.Count) ActiveSheet.Name = "WirelessPivot" ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "Data!R1C1:R50000C30", Version:=xlPivotTableVersion12).CreatePivotTable _ TableDestination:="WirelessPivot!R3C1", TableName:="WirelessPivotTable", _ DefaultVersion:=xlPivotTableVersion12 Sheets("WirelessPivot").Select Cells(3, 1).Select With ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("BU").Orientation = xlRowField.Position = 1 End With With ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("Vendor Name").Orientation = xlRowField.Position = 2 End With With ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("DEPT").Orientation = xlRowField.Position = 3 End With With ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("Wireless Number").Orientation = xlRowField.Position = 4 End With With ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("User Name").Orientation = xlRowField.Position = 5 End With With ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("Equipment Make").Orientation = xlRowField.Position = 6 End With With ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("Equipment Model").Orientation = xlRowField.Position = 7 End With

Excel VB Code (cont.) With ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("Type of Service").Orientation = xlRowField.Position = 8 End With ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Total Current Charges"), _ "Total Current Charge", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Monthly Voice & Data Charge"), _ "Monthly Voice/Data Access Charge", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Additional Airtime Charge"), _ "Additional Airtime Charges", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Total Data Useage Charge (Excluding Roaming)") _, "Data Usage Charges (Excluding Roaming)", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Additional Feature Charge"), _ "Additional Feature Charges", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Total Equipment Charge"), _ "Equipment Charges", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Long Distance Charge"), _ "LD Charges", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Directory Assistance Charge (411 calls)" _ ), "411 Assistance Charge", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Total Roaming Charge"), _ "Roaming Charges", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Service Level Other Charge"), _ "Other Service Level Charges", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Total Taxes Surcharges and Regulatory Fees"), _ "Taxes Surcharges and Regulatory Fees", xlSum

Excel VB Code (cont.) ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Total Miscellaneous Usage Charge"), _ "Miscellaneous Usage Charge", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Total Non-Communications Charge"), _ "Non-Communications Charge", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Total Messaging Charge"), _ "Messaging Charges", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Total Number of Events"), _ "Total Count of Messages", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Total voice Minutes of Use (MOU)"), _ "Voice Minutes of Use (MOU)", xlSum ActiveSheet.PivotTables("WirelessPivotTable").AddDataField ActiveSheet.PivotTables( _ "WirelessPivotTable").PivotFields("Total KB Data Usage"), _ "Data Usage (Kilobytes)", xlSum ActiveSheet.PivotTables("WirelessPivotTable").TableStyle2 = "PivotStyleMedium9" ActiveSheet.PivotTables("WirelessPivotTable").RowAxisLayout xlTabularRow ' Expand to show all details ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("BU"). _ ShowDetail = True ' Removes the subtotals for wireless numbers, User Name,Equipment Make and type of service ActiveSheet.PivotTables("WirelessPivotTable").PivotFields( _ "Wireless Number").Subtotals = Array(False, False, False, False, False, False, _ False, False, False, False, False, False) ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("User Name"). _ Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _ False, False) ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("Equipment Make"). _ Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _ False, False) ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("Type of Service"). _ Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _ False, False)

Excel VB Code (cont.) ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("Equipment Model"). _ Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _ False, False) ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("Dept"). _ Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _ False, False) ActiveSheet.PivotTables("WirelessPivotTable").PivotFields("Vendor Name"). _ Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _ False, False) ' Formats the dollars amounts Columns("I:V").Select Selection.NumberFormat = "$#,##0.00" ' Formats the number with thousands separator Columns("X:Y").Select Selection.NumberFormat = "#,##0" ' Adjust the column widths Columns("A:A").Select Selection.ColumnWidth = 12 Columns("B:B").Select Selection.ColumnWidth = 18 Columns("C:C").Select Selection.ColumnWidth = 10 Columns("D:D").Select Selection.ColumnWidth = 15 Columns("E:E").Select Selection.ColumnWidth = 25 Columns("F:F").Select Selection.ColumnWidth = 14 Columns("G:G").Select Selection.ColumnWidth = 14 Columns("H:H").Select Selection.ColumnWidth = 14 Columns("I:AA").Select Selection.ColumnWidth = 15

Excel VB Code (cont.) ' Formats the header to wrap text Rows("4:4").Select With Selection.HorizontalAlignment = xlGeneral.VerticalAlignment = xlBottom.WrapText = True.Orientation = 0.AddIndent = False.IndentLevel = 0.ShrinkToFit = False.ReadingOrder = xlContext.MergeCells = False End With Range("A1").Select ActiveWindow.Zoom = 85 With ActiveSheet.PageSetup.PrintTitleRows = "$4:$4".PrintTitleColumns = "" End With ActiveSheet.PageSetup.PrintArea = "" With ActiveSheet.PageSetup.CenterHeader = "&A".LeftMargin = Application.InchesToPoints(0.2).RightMargin = Application.InchesToPoints(0.2).Orientation = xlLandscape.PaperSize = xlPaperLetter.Zoom = False.FitToPagesWide = 1.FitToPagesTall = 999 End With End Sub