An Animated Guide©: Sending SAS files to Excel Concentrating on a D.D.E. Macro.

Slides:



Advertisements
Similar presentations
The essentials managers need to know about Excel
Advertisements

Sensitivity Analysis A systematic way of asking “what-if” scenario questions in order to understand what outcomes could possibly occur that would effect.
CC SQL Utilities.
Axio Research E-Compare A Tool for Data Review Bill Coar.
The INFILE Statement Reading files into SAS from an outside source: A Very Useful Tool!
Statistical Methods Lynne Stokes Department of Statistical Science Lecture 7: Introduction to SAS Programming Language.
Office 2003 Advanced Concepts and Techniques M i c r o s o f t Excel Project 4 Financial Functions, Data Tables, Amortization Schedules, and Hyperlinks.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
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.
WORKING SMART Crystal M. Thomas Henrico County DSS (804) POSSESS Central Region Member October 31, 2007.
Understanding Microsoft Excel
Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Working Smarter, Not Harder with DDE: Did I Really Spend All That Time Just Formatting My Report? Bobby Kidd Vice President Senior Statistical Portfolio.
Data Preparation for Analytics Using SAS Gerhard Svolba, Ph.D. Reviewed by Madera Ebby, Ph.D.
Copyright © 2010, Meta-Xceed, Inc. All rights reserved. BI Flash and all other Meta-Xceed Inc. product or service names are registered trademarks or trademarks.
Lesson 1 – Microsoft Excel The goal of this lesson is for students to successfully explore and describe the Excel window and to create a new worksheet.
SAS SQL SAS Seminar Series
Microsoft ® Office Access ™ 2007 Training Choose between Access and Excel ICT Staff Development presents:
1 Data Manipulation (with SQL) HRP223 – 2010 October 13, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
Microsoft Excel 2003 Illustrated Complete
McGraw-Hill Career Education© 2008 by the McGraw-Hill Companies, Inc. All Rights Reserved. 3-1 Office Excel 2007 Lab 3 Managing and Analyzing a Workbook.
1 Performing Spreadsheet What-If Analysis Applications of Spreadsheets.
Math 3400 Computer Applications of Statistics Lecture 1 Introduction and SAS Overview.
SAS Macro: Some Tips for Debugging Stat St. Paul’s Hospital April 2, 2007.
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
Advanced Lesson 5: Advanced Data Management Excel can import data, or bring it in from other sources and file formats. Importing data is useful because.
Using Visual Basic for Applications in Microsoft Project Sean Vogel.
Computer Literacy BASICS: A Comprehensive Guide to IC 3, 5 th Edition Lesson 19 Organizing and Enhancing Worksheets 1 Morrison / Wells / Ruffolo.
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,
FIX Eye FIX Eye Getting started: The guide EPAM Systems B2BITS.
Microsoft ® Office Excel 2003 Training Using XML in Excel SynAppSys Educational Services presents:
1 Data Manipulation (with SQL) HRP223 – 2010 October 13, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
Lesson 1 – Microsoft Excel * The goal of this lesson is for students to successfully explore and describe the Excel window and to create a new worksheet.
1 Lesson 13 Organizing and Enhancing Worksheets Computer Literacy BASICS: A Comprehensive Guide to IC 3, 3 rd Edition Morrison / Wells.
Microsoft Office Illustrated Introductory, Second Edition with Excel 2003 Getting Started.
Comparison of different output options from Stata
Microsoft Office 2003: Advanced 1 ADVANCED MICROSOFT EXCEL Lesson 17 – Working with Multiple Worksheets and Workbooks.
Spreadsheets 101 What is Excel?. Objectives 1. Identify the parts of the Excel Screen 2. Identify the functions of a spreadsheet 3. Identify how spreadsheets.
Gold – Crystal Reports Introductory Course Cortex User Group Meeting New Orleans – 2011.
YET ANOTHER TIPS, TRICKS, TRAPS, TECHNIQUES PRESENTATION: A Random Selection of What I Learned From 15+ Years of SAS Programming John Pirnat Kaiser Permanente.
Spreadsheets What is Excel?. Objectives 1. Identify the parts of the Excel Screen 2. Identify the functions of a spreadsheet 3. Identify how spreadsheets.
Computing with SAS Software A SAS program consists of SAS statements. 1. The DATA step consists of SAS statements that define your data and create a SAS.
An electronic document that stores various types of data.
1 Data Manipulation (with SQL) HRP223 – 2009 October 12, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
BMTRY 789 Lecture9: Proc Tabulate Readings – Chapter 11 & Selected SUGI Reading Lab Problems , 11.2 Homework Due Next Week– HW6.
1 EPIB 698C Lecture 1 Instructor: Raul Cruz-Cano
Pages Appendix B: Review of ExcelChapter 3 Market Trends & Analysis IBM 320 CAL POLY POMONA IBM320 Market Trends and Analysis Maha Ghosn.
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
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.
Microsoft Excel 2007 Noris Bt. Ismail Faculty of Information and Communication Technology Tel : (Ext 8408) BCOMP0101.
Understanding Microsoft Excel
Understanding Microsoft Excel
Formulas and Functions
Chapter 6 Modifying Cell Styles
SQL and SQL*Plus Interaction
Data Validation and Protecting Workbook
Microsoft Excel 101.
Intro to PHP & Variables
Formatting a Workbook Part 2
Understanding Microsoft Excel
Advanced Microsoft Excel
Microsoft Excel 101.
Microsoft Excel 101.
Topic 8 – Pivot tables and Charts Lesson 1 – Pivot tables
Understanding Microsoft Excel
Data Manipulation (with SQL)
Chapter 6 Modifying Cell Styles
Pivot tables and charts
Computer Science 10 & ICT 9 EXCEL
Presentation transcript:

An Animated Guide©: Sending SAS files to Excel Concentrating on a D.D.E. Macro

GOALS For This Presentation A Quick comparison of various ways to send a SAS table to Excel Walk through a generic D.D.E. macro

Put this DDE macro in your abbreviation file Many excellent papers address this topic Method you use might be a company policy Each method has good/bad features Main deliverable is a generic DDE macro Sending Files to Excel

Proc ExportFast - if in abbreviation file Easily overwrites old workbooks Can write several sheets Not a lot of control - no formatting Difficult to use with a template Writes other formats (csv, Access) Libname Engine Fast - if in abbreviation file Can write to pages or named ranges Can write several sheets Workbook is locked until the libref is cleared Can have problems with data set dimensions not agreeing with dimensions of named ranges Does not overwrite data

Sending Files to Excel DDE Control Easy to use with a template Fast - if in abbreviation file Can write several sheets Obsolete??? Fussy syntax Bad Error messages

Declare parameters Open Excel Sections of the generic D.D.E. macro Open Template - if one exists Clean up working files Filter out ROWS of data on variables that we may not want to send to Excel Drop any extra vars. - Load # of variables we want to export into NoOfVars - create a macro array Print the variables we want to export to the log Put the number of rows in a macro variable and give a bit of notice if the file is getting large Issue the D.D.E. statement to dimension the “writing area” Use a data _null_ and a loop to write the D.D.E. put statement If &SaveNowYN =Y, perform any special writes, make the summary sheet active, save and close

Declare parameters %MACRO Write2XlsV2( /*Write ONE SAS file to ONE Tab of an Excel Workbook*/ FirstWrite= /*A Y here will cause SAS to start Excel. AND MUST be valued as Y for the Macro to open an Excel template - used for first call fo XLS sheet*/,SASFile= /*File to send to the XLSDataTab Xls sheet*/,whereClause= /*a complete where statement eg: %str(where 1=1;) */,DropVars= /*a complete "drop data set option" with () and withOUT semicolon eg; (drop=SortOrder4Prod) */,XLSDataTab= /*Tab to which to write the table in SASFile*/,XLSSmryTab= /*Often,at the end of loading a template, we write one or two pieces of info to a summary tab*/,StartRowNo=5 /*You do not have to start writing in R1C1 -Look at the template- See where the data section starts*/,StartColNo=1 /*You do not have to start writing in R1C1 -Look at the template- See where the data section starts- sometimes we start at column 2*/,templateLoc= /*path to the template. If you value this AND set firstWrite to Y, the macro will open the template*/,SaveNowYN=N /*Saves & Closes Excel. After writing last tab in a multi-tab SS, change to Y & enter path in NewName*/,NewName=C:\Temp\DashBoard.xls /*save the XLS sheet under this name. Saving only happens when SaveNowYN=Y */ );

Open Excel %macro OpenExcel; %if (&FirstWrite =Y) or (&FirstWrite =y) or (&FirstWrite =YES) %then %do; /*This is the first time we write to the XLS sheet - we need to open it*/ options noxwait noxsync; /* The ocmmand below will not work! x "C:\Program Files\Microsoft Office97\Office\EXCEL.EXE"; Use the command dir /X to see the real path */ x "C:\Progra~1\MIFF2D~1\Office\EXCEL.EXE"; DATA _NULL_; Rc = SLEEP(5); RUN; %end; %mend OPenExcel; %OpenExcel;

Open Template - if one exists %macro OpenTemplate; /*This macro can be used to write to several pages to a new SS, or template, and we ONLY open the Template: 1) on the first call and 2) if it exists - We Must tell the macro if this is the first call and if the template exists*/ %if ( (%length(&templateLoc) GT 0 ) /*if GT 0, User has specified a template*/ and ( %upcase(&FirstWrite =Y) or (%upcase(&FirstWrite) =YES )) ) %then %do; /*if above is true, user has specified a template to which he wants to write*/ /*if no template specified, write to the sheet that opens when we open excel*/ Filename Excel dde 'EXCEL|SYSTEM'; data _null_; file excel; put "[open(""&TemplateLoc"")]"; run; %end; %else %do; %put No template specified; %end; %mend OpenTemplate; %OpenTemplate;

Clean up working files %macro CleanUp; %if %sysfunc(exist(GoodObs))NE 0 %then %do; Proc SQL; Drop table GoodObs; quit; %end; %else %do; %put NO table cleanup required - no table to drop; %end; %mend CleanUp; %cleanUp;

Filter out ROWS of data on variables that we may not want to send to Excel /*We might want to filter observations on a variable we do not want to send to excel*/ /*Filter observations ASAP and drop variables in the next step*/ Data GoodObs; set &SASFile; &whereClause; /*<----- Drop rows you do NOT want to print*/ /*This, and multiple calls of the macro, can be used to split files that are too large to fit on an XLS sheet*/ run;

Drop any extra vars. - Load # of variables we want to export into NoOfVars - create a macro array /*for the DDE, We need to know how many obs and variables are in the file we are exporting. Put the number of rows and columns into macro variables so we can dimension the DDE writing area*/ /*Note &DropVars. You do not have to export all the variables */ Proc Contents data=GoodObs&DropVars varnum noprint out=VarsToExport ; run;/* create data set with variables info--*/ proc sort data=VarsToExport ; by varnum; run; data _null_; /*How many variables - This number is used to dimension the DDE "writing" area*/ if 0 then set VarsToExport Nobs=NOfObs; call symput("NoOfVars",Strip(left(NOfObs))); run; %put We have &NoOfVars Variables to export from &&SASFile ; data _null_; /*in the DDE, We need to be able to loop and "write" the names of the variables*/ set VarsToExport(keep=name); /*create an array of macro variables*/ call symput("vn"||left(Put(_n_,7.)),name); /*DANGER hardcode*/ run; After dropping variables, we have a file that contains the variables to export Number of variables in NoOfVars Macro array holding variable names (in order)

Print the variables we want to export to the log %macro LeaveRecord; /*As a debugging tool, write the variables we want to exportin the log*/ %do QC=1 %to &NoOfVars; %put for loop number &qc the variable we write is &&vn&QC; %end; %mend LeaveRecord; %LeaveRecord; Print the macro array holding variable names (in order)

Put the number of rows in a macro variable and give a bit of notice if the file is getting large data _null_; /*How many rows - This number is used to dimension the DDE "writing" area */ if 0 then set &SASFile Nobs=NOfObs; call symput("NoOfRows",Strip(NOfObs)); If NOfObs GT then do; /*DANGER HARDCODE*/ put "*************************"; put "WA" "RNING **"; put "There are " NOfObs " in this file.”; put “ Check to see if the might fill up an XLS sheet"; put "*************************"; end; run; %put After applying the where clause, we want to print &NoOfRows from the file &SASFile; We used to be able to overload Excel.

Issue the D.D.E. statement that dimensions the “writing area” filename blah dde "excel|&XLSDataTab! R&StartRowNo.C&StartColNo.: R%eval(&StartRowNo+&NoOfRows) C%eval(&StartColNo+&NoOfVars)" NOTAB; /* in statement above, we dimensioned the DDE "writing" area */ You do not have to send data to Row1 col1 Calculate the dimensions of the writing area

Use a data _null_ and a loop to write the D.D.E. put statement Data _null_; /*this data null reads the GoodObs table and does the writing to excel*/ set GoodObs&DropVars; file blah LRECL=2050; /*The value of Lrecl can range from 1 to 1,048,576 (1 megabyte). According to the experience of SAS experts, an Lrecl of 8192 covers 99% of the cases that most SAS programmers encounter. */ %macro LoopOverVars; /*use a loop to list all the variable names in the DDE statement*/ put %do k=1 %to &NoOfVars; &&vn&k "09"X %end ; %mend LoopOverVars; %LoopOverVars; run; Use a loop to write the put statement

If &SaveNowYN) =Y, perform any special writes, make the sumamry sheet active, save and close %if (%upcase(&SaveNowYN) =Y) or (%upcase(&SaveNowYN) =YES) %then %do; /*If you tell the macro you have written the last data tab, it will 1)** Write any information to the "summary sheet" 2)** Put the date in Cell A2 of Sheet 1 3)** Save the file under a new name */ filename TwoCells dde "excel|&XLSSmryTab!R1C1:R3C2" NOTAB; /*HARDCODED range*/ /*This writing should not ”Overlap" the range where we write data*/ Data _null_; /*HARDCODE - /*We write the 6 cells below into a 3 by 2*/ file TwoCells LRECL=2050; Put "Put in Cell A1" "09"X "Put in Cell A2" "09"X ; Put "Run On:" "09"X "%sysfunc(Date(),worddate18.)" "09"X ; Put "Put in Cell A3" "09"X "Put in Cell B3" "09"X ; run; filename Final dde "excel|&XLSSmryTab!R1C1:R1C1" NOTAB;/*HARDCODE*/ /*(above) Make &XLSSmryTab active before saving then (below)Save and rename*/ filename outexcel dde "EXCEL|SYSTEM"; data _null_; file outexcel; put "[save.as(""&NewName"")]"; put "[close]"; put "[quit]"; run; %end; %Mend Write2XlsV2;

The End Russ Lavery Thanks to Dr. Steve Bajgier

Presentation Structure Equally likely outcomes of random variables Dice: formulas for EV and variance Preliminary Information The Hypergeometric - We'll get there eventually Coins: formulas for EV and variance Fisher's exact test 3 Linear Combinations of Random Variables The Central limit theorem and dice Combinations of equally likely outcomes Combinations vs Permutations Finally - the hypergeometric itself The product rule for equally likely outcomes What if with Excel The CMH Test What if with Excel