SAS ® Global Forum 2014 March Washington, DC Arthur Tabachneck Thornhill, ON Canada Tom Abernathy New York, NY Matt Kastin Penn Valley, PA.

Slides:



Advertisements
Similar presentations
Introduction to Excel This class is “HANDS-ON” you will need to open up an excel spreadsheet and do examples as you go along. Students will be able to.
Advertisements

MS-Word XP Lesson 7.
Microsoft Excel Presented by ShoWorks Fair Software and Online Entries
© Paradigm Publishing, Inc Excel 2013 Level 2 Unit 2Managing and Integrating Data and the Excel Environment Chapter 7Automating Repetitive Tasks.
WORKING SMART Crystal M. Thomas Henrico County DSS (804) POSSESS Central Region Member October 31, 2007.
Using Macros and Visual Basic for Applications (VBA) with Excel
1 After completing this lesson, you will be able to: Create a workbook. Understand Microsoft Excel window elements. Select cells. Enter text, numbers,
Excel Tutorial 1 Getting Started with Excel
Next Presentation: Presenter: Arthur Tabachneck Copy and Paste from Word or Excel to SAS Art holds a PhD from Michigan State University, has been a SAS.
Understanding Microsoft Excel
Excel Lesson 1 Excel Basics. Task 1  Goals  Learn about Excel  Start Excel  Explore the Excel screen  Explore the Excel workbook  Explore the worksheet.
Objectives 1.Identify the functions of a spreadsheet 2.Identify how spreadsheets can be used. 3.Explain the difference in columns and rows. 4.Locate specific.
Automagically Copying and Pasting Variable Names Arthur Tabachneck Roger DeAngelis Randy Herbison Insurance Bureau of Canada CompuCraft Inc Westat John.
Microsoft Excel 2010 Chapter 7
Spreadsheet. Objectives Create a new blank workbook. Create a new blank workbook. Identify user interface elements that you can use to accomplish basic.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
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.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
Microsoft Excel Spreadsheet Review. Templates  Templates can be produced for the following elements:  Text and Graphics  Formatting Information – Layouts,
Creating a Web Site to Gather Data and Conduct Research.
Spreadsheets in Finance and Forecasting Presentation 9 Macros.
Learning Objectives What is a spreadsheet and what is the difference between a spreadsheet and a worksheet? Start Excel, open an existing spreadsheet,
XP New Perspectives on Integrating Microsoft Office XP Tutorial 2 1 Integrating Microsoft Office XP Tutorial 2 – Integrating Word, Excel, and Access.
· Adding and Renaming Worksheets
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Lesson 1 Introduction.
ISU Basic SAS commands Laboratory No. 1 Computer Techniques for Biological Research Animal Science 500 Ken Stalder, Professor Department of Animal Science.
Define your Own SAS® Command Line Commands Duong Tran – Independent Contractor, London, UK Define your Own SAS® Command Line Commands Duong Tran – Independent.
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.
Chapter 28. Copyright 2003, Paradigm Publishing Inc. CHAPTER 28 BACKNEXTEND 28-2 LINKS TO OBJECTIVES Table Calculations Table Properties Fields in a Table.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Chapter 7 Creating Templates, Importing Data, and Working with SmartArt, Images, and Screen Shots Microsoft Excel 2013.
Instructions for using this template. Remember this is Jeopardy, so where I have written “Answer” this is the prompt the students will see, and where.
SAS ® Global Forum 2014 March Washington, DC Arthur Tabachneck Thornhill, ON Canada Tom Abernathy New York, NY Matt Kastin Penn Valley, PA.
Microsoft Excel Consolidation. Contents Introduction to Multiple Workbook Applications Working with Multiple Workbook Applications using normal keyboard.
SAS ® Global Forum 2014 March Washington, DC Arthur Tabachneck Toronto, ON Canada Tom Abernathy New York, NY Matt Kastin Penn Valley, PA.
Chapter 8 Using Document Collaboration, Integration, and Charting Tools Microsoft Word 2013.
Understanding Microsoft Excel
Using a set-up file to read ASCII data into Stata
Excel Tutorial 8 Developing an Excel Application
June 17, 2009 Office 2007 Tips & Tricks.
Understanding Microsoft Excel
Using Office Backstage
5.2 Microsoft Excel.
Using Macros, Switchboards, PivotTables, and PivotCharts
International Computer Driving Licence Syllabus version 5.0
Created by Stacey Wilson
Downloading and Preparing a StudentVoice File for SPSS
Microsoft Excel Basics
Spreadsheet Presentation
Managing Worksheets And Workbooks
After completing this lesson, you will be able to:
Access Creating a Database
MS-Excel Part 1.
Sharing Data among Applications
Access Creating a Database
Microsoft Excel 101.
Managing Multiple Worksheets and Workbooks
Understanding Microsoft Excel
5.2 Microsoft Excel.
Chapter 1: Introduction to SAS
Exploring Microsoft® Access® 2016 Series Editor Mary Anne Poatsy
Microsoft Excel All editions of Microsoft office.
Microsoft Excel 101.
Sirena Hardy HRMS Trainer
Eviews Tutorial for Labor Economics Lei Lei
Understanding Microsoft Excel
Fundamentals of Using Excel
Chapter 8 Using Document Collaboration and Integration Tools
Lesson 13 Working with Tables
Microsoft Excel 2007 – Level 2
Presentation transcript:

SAS ® Global Forum 2014 March Washington, DC Arthur Tabachneck Thornhill, ON Canada Tom Abernathy New York, NY Matt Kastin Penn Valley, PA

2 Have you ever run into problems trying to export SAS datasets to Excel? Question

3 Regardless of whether you said yes or no: Would you like to be able to: export without running into 32/64 bit clashes or needing SAS/Access for PC File Formatsexport without running into 32/64 bit clashes or needing SAS/Access for PC File Formats export by simply clicking on a file in the SAS Explorer windowexport by simply clicking on a file in the SAS Explorer window include or exclude the row that contains variable namesinclude or exclude the row that contains variable names export a table to a specific rangeexport a table to a specific range add a table to an existing worksheetadd a table to an existing worksheet copy a file to your system's clipboardcopy a file to your system's clipboard

4 what the solution looks like Right click on a dataset name in the SAS Explorer window

5 Then select the desired Action (i.e., press hot key (E) or point and left-click)

6 The Workbook will automagically be created

7 Left-click anywhere in the SAS Explorer Window how to get those capabilities

8 Left-click on Tools, move your mouse to Options→Explorer how to get those capabilities

9 Left-click on Members how to get those capabilities

10 Then double left-click on Table how to get those capabilities

11 left-click on Add an Add Action screen will appear and an Add Action screen will appear how to get those capabilities

12 Type the text you want to add to the menu &Export to Excel Action: &Export to Excel It will appear on the menu as: Export to Excel Note: You can make one character a hotkey by preceding it with an ampersand how to get those capabilities

13 Left click in the Action Command box &Export to Excel how to get those capabilities

14 Action Commands only accept up to 255 characters The 255 character limitation can be circumvented by submitting a macro When using gsubmit in an Action Command, everything between the two single quotes will be submitted some key points about the Action Command % has a special meaning in Action Commands thus, if you need to use a % (e.g., to call a macro), use two %s When a SAS dataset is selected %8b and %32b can be used to refer to the libname and filename, respectively

15 which can produce 9 different actions dependent upon how it is called Action #1: gsubmit '%exportxl(%8b, %32b, P, YES, NO)'; will run proc export on the dataset Note: Requires SAS/Access for PC File Formats gsubmit '%exportxl(%8b,%32b,P,YES,NO)'; &Export to Excel our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange

16 which can produce 9 different actions dependent upon how it is called Action #2: gsubmit '%exportxl(%8b, %32b, S, YES, NO)'; creates an Excel workbook with variable names in 1 st row Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,S,YES,NO)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Excel with Variable &Names

17 which can produce 9 different actions dependent upon how it is called Action #3: gsubmit '%exportxl(%8b, %32b, S, YES, YES)'; creates an Excel workbook with variable names in 1 st row and lets you indicate the upper left cell where range begins Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,S,YES,YES)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Excel Range with Variable &Names

18 which can produce 9 different actions dependent upon how it is called Action #4: gsubmit '%exportxl(%8b, %32b, S, NO, NO)'; creates an Excel workbook without variable names in 1 st row Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,S,NO,NO)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Excel w/&o Variable Names

19 which can produce 9 different actions dependent upon how it is called Action #5: gsubmit '%exportxl(%8b, %32b, S, NO, YES)'; creates an Excel workbook without variable names in 1 st row and lets you indicate the upper left cell where range begins Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,S,NO,YES)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Excel Range w/o Variable Names

20 which can produce 9 different actions dependent upon how it is called Action #6: gsubmit '%exportxl(%8b, %32b, A, YES, YES)'; adds to an existing Excel workbook with variable names in top row and you indicate upper left cell where range begins Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,A,YES,YES)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange &Add to Excel Range with Variable Names

21 which can produce 9 different actions dependent upon how it is called Action #7: gsubmit '%exportxl(%8b, %32b, A, NO, YES)'; adds to existing Excel workbook w/o variable names in top row and you indicate the upper left cell where range begins Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,A,NO,YES)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Excel Range w/o Variable Names

22 which can produce 9 different actions dependent upon how it is called Action #8: gsubmit '%exportxl(%8b, %32b, C, YES, NO)'; copies dataset to clipboard with variable names in 1 st row Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,C,YES,NO)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Clipboard with Variable &Names

23 which can produce 9 different actions dependent upon how it is called Action #9: gsubmit '%exportxl(%8b, %32b, C, NO, NO)'; copies dataset to clipboard w/o variable names in 1 st row Note: Actions 2-9 only require base SAS gsubmit '%exportxl(%8b,%32b,C,NO,NO)'; our paper presents a SAS macro: %exportxl libnm filenmtype usenamesrange Export to Clipboard w/o Variable &Names

24 click on OK to exit the Add Action screen gsubmit '%exportxl(%8b,%32b,P,YES)'; &Export to Excel after you have entered the action command

25 click on OK to exit the Table Options screen then,

26 Once you have completed those steps whenever you right click on a file in the SAS Explorer window, and select an action, the action will be taken

27 if you use one of the Actions with range=YES* enter the upper left cell where the range should begin and then press your key *Note: not applicable to Action #1 as proc export doesn't support outputting to a range the following screen will appear when you select the action

28 and, if you use Actions 2-7 i.e., the program will ask you whether it should, or shouldn't, replace the existing file if the workbook already exists, a screen like the following will appear

29 Save the file as exportxl.sas in a directory that exists in your SASAUTOS* path * see: where to get the macro Copy the SAS program from: User: Art297

30 %macro exportxl(libnm,filenm,type,usenames,range); %let filepath=%sysfunc(pathname(&libnm.)); %if &type. eq P %then %do; proc export data=&libnm..&filenm. outfile= "&filepath.\&filenm..xlsx" dbms=xlsx replace; run; %end; how the macro works libnm filenmtype usenames %8b %32b P for Proc Export S for vbs script A(dd) to existing workbook C for Clipboard include row with variable names YES NO %let filepath=%sysfunc(pathname(&libnm.)); whether table should be written to a range YES NO range

31 %macro exportxl(libnm,filenm,type,usenames,range); %let filepath=%sysfunc(pathname(&libnm.)); %if &type. eq P %then %do; proc export data=&libnm..&filenm. outfile= "&filepath.\&filenm..xlsx" dbms=xlsx replace; run; %end; how the macro works

32 %else %do; proc fcmp outlib=work.func.util; function c2cb(lib $,mem $, usenm $); rc=filename('clippy',' ','clipbrd'); if rc ne 0 then return(1); fid=fopen('clippy','o',0,'v'); if fid eq 0 then do; rc = filename( 'clippy' ); return(2); end; dsid=open(catx('.',lib,mem)); if dsid eq 0 then do; rc=fclose(fid); rc=filename('clippy'); return(3); end; how the macro works

33 %else %do; proc fcmp outlib=work.func.util; function c2cb(lib $,mem $, usenm $); rc=filename('clippy',' ','clipbrd'); if rc ne 0 then return(1); fid=fopen('clippy','o',0,'v'); if fid eq 0 then do; rc = filename( 'clippy' ); return(2); end; dsid=open(catx('.',lib,mem)); if dsid eq 0 then do; rc=fclose(fid); rc=filename('clippy'); return(3); end; how the macro works Open the clipbrd so that we can write to it

34 %else %do; proc fcmp outlib=work.func.util; function c2cb(lib $,mem $, usenm $); rc=filename('clippy',' ','clipbrd'); if rc ne 0 then return(1); fid=fopen('clippy','o',0,'v'); if fid eq 0 then do; rc = filename( 'clippy' ); return(2); end; dsid=open(catx('.',lib,mem)); if dsid eq 0 then do; rc=fclose(fid); rc=filename('clippy'); return(3); end; how the macro works Open the SAS dataset so that we can read it

35 nvar=attrn(dsid,'nvar'); array v[1] /nosymbols; call dynamic_array(v,nvar); do i = 1 to nvar; v[i]=ifn( vartype(dsid,i)='C',1,2); if usenm eq 'YES' then do; if i gt 1 then rc=fput(fid,'09'x); rc=fput(fid,varname(dsid,i)); end; if usenm eq 'YES' then rc=fwrite(fid); do i=1 to attrn(dsid,'nlobs'); rc=fetchobs(dsid,i); do j=1 to nvar; if j gt 1 then rc=fput(fid,'09'x); if (v[j] eq 1) then rc=fput(fid, getvarc(dsid,j)); else do; fmt=varfmt(dsid,j) ; if missing(fmt) then fmt='best12.'; rc=fput(fid,putc(putn(getvarn(dsid,j),fmt),'$char12.')); end; rc=fwrite(fid); end; how the macro works Get number of variables and declare array

36 nvar=attrn(dsid,'nvar'); array v[1] /nosymbols; call dynamic_array(v,nvar); do i = 1 to nvar; v[i]=ifn( vartype(dsid,i)='C',1,2); if usenm eq 'YES' then do; if i gt 1 then rc=fput(fid,'09'x); rc=fput(fid,varname(dsid,i)); end; if usenm eq 'YES' then rc=fwrite(fid); do i=1 to attrn(dsid,'nlobs'); rc=fetchobs(dsid,i); do j=1 to nvar; if j gt 1 then rc=fput(fid,'09'x); if (v[j] eq 1) then rc=fput(fid, getvarc(dsid,j)); else do; fmt=varfmt(dsid,j) ; if missing(fmt) then fmt='best12.'; rc=fput(fid,putc(putn(getvarn(dsid,j),fmt),'$char12.')); end; rc=fwrite(fid); end; how the macro works Assign values to array based on variable type Assign variable names

37 nvar=attrn(dsid,'nvar'); array v[1] /nosymbols; call dynamic_array(v,nvar); do i = 1 to nvar; v[i]=ifn( vartype(dsid,i)='C',1,2); if usenm eq 'YES' then do; if i gt 1 then rc=fput(fid,'09'x); rc=fput(fid,varname(dsid,i)); end; if usenm eq 'YES' then rc=fwrite(fid); do i=1 to attrn(dsid,'nlobs'); rc=fetchobs(dsid,i); do j=1 to nvar; if j gt 1 then rc=fput(fid,'09'x); if (v[j] eq 1) then rc=fput(fid, getvarc(dsid,j)); else do; fmt=varfmt(dsid,j) ; if missing(fmt) then fmt='best12.'; rc=fput(fid,putc(putn(getvarn(dsid,j),fmt),'$char12.')); end; rc=fwrite(fid); end; how the macro works Write variable names

38 nvar=attrn(dsid,'nvar'); array v[1] /nosymbols; call dynamic_array(v,nvar); do i = 1 to nvar; v[i]=ifn( vartype(dsid,i)='C',1,2); if usenm eq 'YES' then do; if i gt 1 then rc=fput(fid,'09'x); rc=fput(fid,varname(dsid,i)); end; if usenm eq 'YES' then rc=fwrite(fid); do i=1 to attrn(dsid,'nlobs'); rc=fetchobs(dsid,i); do j=1 to nvar; if j gt 1 then rc=fput(fid,'09'x); if (v[j] eq 1) then rc=fput(fid, getvarc(dsid,j)); else do; fmt=varfmt(dsid,j) ; if missing(fmt) then fmt='best12.'; rc=fput(fid,putc(putn(getvarn(dsid,j),fmt),'$char12.')); end; rc=fwrite(fid); end; how the macro works Get data and formats and write tab-delimited records

39 rc=fclose(fid); rc=close(dsid); rc=filename('clippy'); return(0); endsub; quit; %local cmplib; %let cmplib=%sysfunc(getoption(cmplib)); options cmplib=(work.func); %put %sysfunc(c2cb(&libnm,&filenm,&usenames)) ; options cmplib=(&cmplib); how the macro works Cleanup

40 how the macro works rc=fclose(fid); rc=close(dsid); rc=filename('clippy'); return(0); endsub; quit; %local cmplib; %let cmplib=%sysfunc(getoption(cmplib)); options cmplib=(work.func); %put %sysfunc(c2cb(&libnm,&filenm,&usenames)) ; options cmplib=(&cmplib); Get options Run Function Reset Options

41 %if &range. eq YES %then %do; data _null_; window range rows=8 columns=80 irow=1 icolumn=2 color=black 'Enter the upper left cell where range should begin (e.g. D5): ' color=gray range $8. required=yes attr=underline color=yellow; DISPLAY range blank; call symputx('range',range); stop; run; %end; %else %do; %let range=A1; %end; how the macro works Display window to let users identify range

42 %if &range. eq YES %then %do; data _null_; window range rows=8 columns=80 irow=1 icolumn=2 color=black 'Enter the upper left cell where range should begin (e.g. D5): ' color=gray range $8. required=yes attr=underline color=yellow; DISPLAY range blank; call symputx('range',range); stop; run; %end; %else %do; %let range=A1; %end; how the macro works Otherwise, set default range

43 %if &type. eq A or &type. eq S %then %do; data _null_; length script filevar $256; script = catx('\',pathname('WORK'),'PasteIt.vbs'); filevar = script; script="'"||'cscript "'||trim(script)||'"'||"'"; call symput('script',script); file dummy1 filevar=filevar recfm=v lrecl=512; put 'Dim objExcel'; put 'Dim Newbook'; put 'Set objExcel = CreateObject("Excel.Application")'; %if &type. eq S %then %do; put 'Set Newbook = objExcel.Workbooks.Add()'; put 'Newbook.Sheets("Sheet1").Select'; script=catt('Newbook.Sheets("Sheet1").Name = "',"&filenm.",'"'); put script; %end; %else %do; script=catt('Set Newbook = objExcel.Workbooks.Open("',"&filepath.\&filenm..xlsx",'")'); put script; script=catt('Newbook.Sheets("',"&filenm.",'").Select'); put script; %end; put 'objExcel.Visible = True'; script=catt('Newbook.Sheets("',"&filenm.",'").Range("',"&Range.",'").Activate'); put script; script=catt('Newbook.Sheets("',"&filenm.",'").Paste'); put script; script=catt('Newbook.SaveAs("',"&filepath.\&filenm..xlsx",'")'); put script; put "Newbook.Close"; put 'objExcel = Nothing'; put 'Newbook = Nothing'; run; how the macro works If type eq A or S create a VBS script called PasteIt.vbs Dim objExcel Dim Newbook Set objExcel = CreateObject("Excel.Application") Set Newbook =objExcel.Workbooks.Open("c:\art\Class.xlsx") Newbook.Sheets("Class").Select objExcel.Visible = True Newbook.Sheets("Class").Range("f3").Activate Newbook.Sheets("Class").Paste Newbook.SaveAs("c:\art\Class.xlsx") Newbook.Close objExcel = Nothing Newbook = Nothing

44 data _null_; call system(&script.); run; %end; %mend exportxl; how the macro works Run the VBS script where &script. = cscript WorkDirectoryPath\PasteIt.vbs

45 What the ExportXL macro is and where you can download it How the macro works How you can set up Action Commands to call the macro to accomplish things that you can't do with proc export summary of what I just presented: How you can create Action Commands in the SAS Explorer window

46 Export to Excel Copy Variable Names to Clipboard Run Proc Contents Get descriptive statistics Show all correlations Run Proc Means Print bar charts Run Proc Freq and, if you want, you can use the same method to add all of your repeated tasks to the SAS Explorer menu

47 stand alone version (exportxlsx.sas) also available on the paper's web page uses named parameters extra functionality: choice of variable labels or names, specify separate output path and indicate whether file should be replaced callable from base SAS or EG ability to write multiple tables to same worksheet uses PROC EXPORT-like parameter names (e.g., data, outfile, sheet and replace)

Your comments and questions are valued and encouraged Contact the Authors Arthur Tabachneck, Ph.D. myQNA, Inc. Thornhill, ON Tom Abernathy Pfizer, Inc. New York, NY Matt Kastin I-Behavior, Inc. Penn Valley, PA