Example: marketing

012-2011: Tips and Techniques for Automating the …

1 Paper 012-2011 tips and Techniques for Automating the SAS Add-In for Microsoft Office with Visual Basic for Applications Tim Beese, SAS Institute Inc., Cary, NC ABSTRACT Do you want to run SAS Stored Processes in Microsoft Excel and set the prompt values from your worksheet? Do you want to filter your SAS data sets based on the values of specific cells? Do you want to customize your Microsoft Office content with buttons and other controls that allow you to open data, run SAS code and refresh results? This is now possible! The SAS Add-In for Microsoft Office adds many new features that allow users to interact with their content through Visual Basic for Applications (VBA). Users can insert and refresh data, stored processes, and reports using VBA. It is also possible to provide prompt values to stored processes, filter and sort strings for data, and control where results are displayed. Combining these features with the existing functionality provided by VBA in Microsoft Office, integration of SAS within Microsoft Office will become easier and more powerful!

1 Paper 012-2011 Tips and Techniques for Automating the SAS® Add-In for Microsoft Office with Visual Basic for Applications Tim Beese, SAS Institute Inc., Cary, NC ...

Tags:

  Automating, Tips, Technique, Tips and techniques for automating the, Tips and techniques for automating the sas

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of 012-2011: Tips and Techniques for Automating the …

1 1 Paper 012-2011 tips and Techniques for Automating the SAS Add-In for Microsoft Office with Visual Basic for Applications Tim Beese, SAS Institute Inc., Cary, NC ABSTRACT Do you want to run SAS Stored Processes in Microsoft Excel and set the prompt values from your worksheet? Do you want to filter your SAS data sets based on the values of specific cells? Do you want to customize your Microsoft Office content with buttons and other controls that allow you to open data, run SAS code and refresh results? This is now possible! The SAS Add-In for Microsoft Office adds many new features that allow users to interact with their content through Visual Basic for Applications (VBA). Users can insert and refresh data, stored processes, and reports using VBA. It is also possible to provide prompt values to stored processes, filter and sort strings for data, and control where results are displayed. Combining these features with the existing functionality provided by VBA in Microsoft Office, integration of SAS within Microsoft Office will become easier and more powerful!

2 WHAT IS SCRIPTING? Scripting is the ability to write programmatic steps to reproduce a series of operations on an application. Microsoft Office is scriptable, featuring a rich interface that can be accessed from VBA. The SAS Add-In for Microsoft Office also has an interface that users can access from their VBA scripts in Microsoft Excel, Word, and PowerPoint. The SAS Add-In for Microsoft Office has had a scriptable interface for several releases, but the functionality was limited. The Create Schedule feature of the SAS Add-In for Microsoft Office creates a script and schedules it using the Microsoft Windows Task Scheduler. When the task runs, the script executes the SAS Add-In for Microsoft Office code and the content is refreshed. WHY IS SCRIPTING USEFUL? The purpose of scripting is to make your job easier. If you have a series of steps that you do over and over that involve manipulating SAS content in a Microsoft Office document, then writing a script can make that process easier.

3 Suppose you have a process in place to import sales data each morning into your Microsoft Excel workbook. Once you have that data available, you copy it to your SAS server so that it is available to everyone. Then you have a series of three stored processes that you run. These stored processes analyze the data and provide specific information about it. Reproducing these steps each day is not difficult, but it can be time consuming. With a VBA script, you can automate all of these steps instead of having to manually perform each step. Scripting can also make other people s work easier. Users who understand VBA very well and who know which SAS Add-In for Microsoft Office code must run in order to perform some specific analysis can define macros in their Microsoft Office document. These macros connect to SAS and interact with the SAS Add-In for Microsoft Office. Users can also create buttons or menu items in Microsoft Office that invoke their macros.

4 Then, the end users who receive the Microsoft Office document simply click the right buttons, and everything will be started for them! HOW DO YOU SCRIPT THE SAS ADD-IN FOR MICROSOFT OFFICE To script the SAS Add-In for Microsoft Office, you must first go to the Visual Basic Editor in Microsoft Office. In Microsoft Office 2003, select Tools > Macros > Visual Basic Editor. In Microsoft Office 2007 and 2010, from the Developer tab, click Visual Basic. The Developer tab does not appear by default. To display this tab in Microsoft Office 2007, you go to the Excel Options dialog and select the Popular page. From there you select Show Developer tab in the Ribbon . In Microsoft Office 2010 you go to the Excel Options dialog and choose the Customize Ribbon page. On this page you can select the Developer tab as one that should be shown. You may also use Alt+F11 to bring up the Visual Basic Editor directly.

5 Once you have the Visual Basic Editor open, you will want to insert a new module. Select the VBAP roject node in the Explorer view, and then choose Insert > Module. A new Module node is added to the tree under your project. This is where you can insert your VBA code. Applications DevelopmentSASG lobalForum2011 2 In order to use Intellisense within the VBA editor, you must add a reference to the SAS Add-In for Microsoft Office. To do this, select Tools > References. Find SAS Add-In for Microsoft Office in the list, and select the check box next to it. Now you have a reference to the SAS Add-In for Microsoft Office and you are ready to begin writing VBA code. Now is a good time to test things out and make sure that your connection to the API is working. You can write a simple VBA script to verify that you are successfully interacting with the SAS Add-In for Microsoft Office. Enter the following code into the VBA Editor, and then run the macro (by pressing F5): 01 Sub TestConnectionToSas() 02 Dim sas As SASE xcelAddIn 03 Set sas = (" ").

6 Object 04 05 End Sub When you run this macro, you will tell the SAS Add-In for Microsoft Office to display a message box that simply reads, Hello World . If you can see this message , then you are successfully calling into the SAS Add-In for Microsoft Office. Let s look at this simple example a little closer. You will very likely be using the syntax on lines 2 and 3 very often. This is how you get your connection to the API in the SAS Add-In for Microsoft Office. On line 2 you are defining a variable of type SASE xcelAddIn. This is the main object that you will be making your calls against while Automating the SAS Add-In for Microsoft Office. Line 3 is where we get our reference to the object. To get this, we must access the list of all the COM Add-Ins that are currently loaded in Microsoft Office. We know that ours has a progId of , so we can search for that COMAddIn. The search returns us a COMAddIn object, which is the Microsoft Office wrapper for the SAS Add-In for Microsoft Office.

7 To get directly to the SAS Add-In for Microsoft Office object behind the wrapper, you must add the .Object property to the call so that you can get directly to the SAS API. When you are working with Microsoft Word you will define a SASWordAddIn object, and in Microsoft PowerPoint you will define a SASP owerPointAddIn object. Similarly, when you look for the item in the list of COM Add-Ins, you will look for and SCRIPTING A REFRESH Let s do something a little more involved. In the past several releases of the SAS Add-In for Microsoft Office, you have been able to script a refresh of SAS content, though you might not have been aware of this ability. You can call refresh for any of the SAS content in your Microsoft Office document (except for content displayed in the OLAP Viewer). There are different ways to do this. Let s look at an example that shows many ways to refresh different content in the Microsoft Office document.

8 01 Sub RefreshSasContent() 02 Dim sas As SASE xcelAddIn 03 Set sas = (" ").Object 04 05 ThisWorkbook 06 07 ("A1") 08 "Bar_Chart" 09 End Sub The preceding code will refresh the same content five different ways. Before you can run this macro and refresh the content, you must create some content in your Excel workbook. For this example I ran a Bar Chart task and placed the contents in cell A1 of Sheet1. As I run this macro, I can look at the footnote and see the time get updated each time I call refresh. Lines 2 and 3 should look familiar; they are getting a reference to the SAS Add-In for Microsoft Office API from Excel. Line 4 simply calls Refresh with no parameters. This will refresh everything that the SAS Add-In for Microsoft Office can find, in every workbook. If you have four workbooks open and each one has SAS content, everything will get refreshed. Applications DevelopmentSASG lobalForum2011 3 Line 5 refreshes everything in the workbook where this macro is defined.

9 The ThisWorkbook syntax returns an Excel workbook object corresponding to the workbook where your macro was added. With this call, all the SAS content on any sheet in this workbook will be refreshed. Line 6 refreshes everything on the active worksheet. Again, we use the ThisWorkbook syntax to get an Excel workbook object, and then access the ActiveSheet property to get an Excel worksheet object. All of the SAS content that appears on the worksheet will be refreshed. Line 7 refreshes the SAS content that exists in the given range. This uses the Sheet1 syntax to get a reference to the Excel Worksheet object defined as Sheet1, and then uses the Range method to get an Excel Range object for cell A1. This is the equivalent to selecting the given cell and pressing the Refresh button on the SAS Ribbon in Microsoft Office. Line 8 refreshes a specific SAS result. Each result that the SAS Add-In for Microsoft Office renders in the Microsoft Office document has an object name, and you can pass this object name to the Refresh method.

10 To find out what the object name is, simply place the selection inside the SAS content, and then select Properties from the SAS Ribbon. The Object Name will appear on the General tab of the Properties dialog box. If you are not sure where your content is, you can select Manage Content from the SAS Ribbon to see all of the content in the active Microsoft Office document, and you can view the properties from there. The Refresh functionality has been available for the past several releases. However, with the SAS Add-In for Microsoft Office, you can also call Modify instead of Refresh. All of the same options are available for which content to modify. The difference is that instead of simply refreshing the content, the script will display any prompts for the content, just as if you had selected Modify from the SAS user interface. USING VBA TO INSERT SAS DATA INTO YOUR WORKBOOK Now let s look at some more of the new functionality in the SAS Add-In for Microsoft Office that enables you to insert new content into your Microsoft Office document.


Related search queries