Many MS Excel Worksheets – Read Them Without Typing Their Names Ravi Vaylay, PhD, Cybrid, Inc Abstract: The objective of this paper is to present a VBA (Visual Basic for Applications) macro in MS Excel and further SAS® programming to read several worksheets of a single MS Excel workbook without typing their names. The worksheet names of an interested workbook are obtained by running a VBA macro stored in a separate MS Excel file through a SAS program. In the same SAS program, Proc Import procedure is used to read all worksheets and create SAS data sets. Introduction: Needless to mention, MS Excel is the most popular data entry option. It is quite common for SAS programmers to receive data in MS Excel format. Each MS Excel workbook can hold 255 worksheets. Many a times, data is given to SAS programmers in one MS Excel workbook with several worksheets. It may be quite tedious and error prone for a SAS programmer to type each worksheet name and read it individually into SAS. There are quite a few papers presented on SAS and Excel. Roper (2000) demonstrated running a VBA macro from SAS program. Vyerman (2002) introduced and discussed a X4ML (Excel version 4 Macro Language) technique of obtaining worksheet names but was of the opinion that it may not work if there are too many worksheets. So far, automation of reading several worksheets is not yet presented. Some of the features of X4ML no longer work as VBA has replaced it. Therefore, I used VBA macros. In this present paper, I will present and discuss a technique to create list of all the worksheets present in a MS Excel workbook. After we obtain the list of the worksheet names, we can read all of the worksheets iteratively using commonly used procedure – Proc Import. Methodology Create List of Worksheet Names: For this kind of task, the most important aspect is to obtain a list of all worksheets present in a workbook. To accomplish this task, the following VBA code is used.
Sub worksheet_names() Dim WS As Worksheet Sheets.Add.Name = "Worksheet_Names" For Each WS In Worksheets Cells(WS.Index, 1).Value = WS.Name Next WS End Sub

The above code may be created in a new MS Excel file. Steps to do it are: Open a new MS Excel file > Tools > Macro > (type a name for macro, in the above, it is worksheet_names in the first line), Create. Now, Visual Basic editor opens up with Sub

Comments are given within the SAS program. Step 4: Read all worksheet names from the newly created worksheet in the data MS Excel file and close all opened MS Excel files. The new worksheet is called as Worksheet_Names in the above example. Prior to running the VBA macro using SAS. The row range is given as r256 which is the maximum as there are 255 maximum allowed worksheets and 1 inserted new worksheet. Conclusions: Once after an Excel file is created with VBA macros. The SAS Program The required SAS program is presented in the appendix 1. and if the security level is medium.NESUG 17 Posters worksheet_names(). The VBA macro works if there are 255 worksheets (the maximum allowed) in a workbook. please restore the Security to High or Medium so that a user may be alerted if there are any viruses. The SAS program may be broadly divided into the following steps: Step 1: Define macro variables for Excel file containing VBA macros. Step 5: Assign a macro variable dynamically for each worksheet name and to the total number of worksheets. and to the range of worksheet names after they are output. obtains its name and outputs to the newly created worksheet (Worksheet_Names) starting from 1st row of 1st column. Step 2: Open the required 2 Excel files – one with the VBA macros and the other from which data needs to be read. it can always be used as and when the need arises. Type the above code starting from 2nd line till the last before line. Select Low. If one is using Office XP. Both files need to be opened in order to run the VBA macro and to obtain the names of the worksheets of the data Excel file. 2. Step 6: Create a macro for proc import and read all worksheets iteratively. loops through each worksheet. VBA macro name. it would be better to disable the Security feature of MS Excel. After reading data from MS Excel worksheets. While running SAS program. 2 . a prompt will appear and a user can simply enable the macros. the Excel file from which data has to be read. This enables the proc import to read each worksheet iteratively and as well as to stop after reading the last worksheet. there is no need to disable the security feature. Step 3: Run the VBA macro from within the SAS program. For this. Note: 1. This code when run through a SAS program. go to Tools > Macro > Security.

C. * Macro name for Excel file containing VBA macro. options noxsync noxwait. Appendix 1: **********************************************************************. * Opens the Data XL File.xls!worksheet_names. %let xl_data_file = C:\Ravi\nesug\data. * Step 2: Open the Required Excel Files *.NESUG 17 Posters References: Roper. run. PhD Cybrid. *** Let SAS sleep for a while ****************************************. PA 17109 e-mail: ravi@cybridinc. %let xl_macro_file = c:Ravi\nesug\worksheet_names. *Macro name for Excel file containing data to be read. 3 . ® indicates USA registration. Creating Custom Excel Workbooks from Base SAS with Dynamic Data Exchange: A Complete Walkthrough. Suite 253 Harrisburg. X "&xl_macro_file". Using SAS and DDE to execute VBA macros in Microsoft Excel.xls]Worksheet_Names!r1c1:r256c1. in the USA and other countries. **********************************************************************. * Opens the Macro XL File. X "&xl_data_file" SAS® and all other SAS Institute Inc. %let xl_macro = worksheet_names. * Macro Name for the list of the worksheet names.xls. Proceeding of 27th Annual SAS Users Group International Conference. data _null_. Vyerman.A (2000). paper 98. product or service names are registered trademarks or trademarks of SAS Institute Inc. **********************************************************************. Inc 4807 Jonestown Road. Proceeding of the 25th Annual SAS Users Group International Conference. rc = sleep(10). %let xl_ref = c:\Ravi\[data. paper 27. *Macro name for VBA macro within the Excel file. Contact Information: Ravi Vaylay. **********************************************************************. * Step 1: Create Macro Variables *. K (2002).

options noxsync noxwait. infile excel2 dlm='09'x.compress(Worksheet_Names)). * Step 3: Run the VBA Macro present in MS Excel File *. data sh_names. set sh_names end=last. data _null_. **********************************************************************. * number of worksheets. if worksheet_names = ‘Worksheet_Names’ then delete. * Delete the observation with value=’Worksheet_Names’ as this is the *.NESUG 17 Posters **********************************************************************.close(false)]'. data _null_. run. filename sas2xl dde 'excel|system'. Create a counter variable to count the number*. * name of the newly created worksheet file. **********************************************************************. input worksheet_names : $31.trim(left(put(i.8. put "[RUN(""&&xl_macro"")]". *.8. put '[quit()]'. **********************************************************************. **********************************************************************. run. data _null_. *** Let SAS sleep for a while ****************************************. * The length of the worksheet names can be up to 31 characters *. * Step 4: Read the Worksheet Names from Excel Data File *. * Step 5: Assign Macro Variable to each worksheet name and to total *.. *** Let SAS sleep for a while Again **********************************. call symput ('n'||trim(left(put(i. **********************************************************************. file sas2xl. options noxsync noxwait.)))). put '[error(false)]'. *** Close All Excel Files ********************************************. put '[file. run. rc = sleep(10). data _null_. file sas2xl. run. data _null_. run. 4 . if last then call symput('total'. rc = sleep(10).))). filename excel2 dde "excel|&&xl_ref". * of each iteration and total iterations *. i + 1.

* Step 6: Macro To Read All Data Worksheets *. quit. 5 . %end.NESUG 17 Posters **********************************************************************. %read_xl_sheets. %macro read_xl_sheets. run. %do i=1 %to &total. * Invoke the Macro *. run. **********************************************************************. **********************************************************************. proc import datafile = "&xl_data_file" out = &&n&i dbms = excel2000 replace. getnames=yes. **********************************************************************. %mend read_xl_sheets. sheet = "&&n&i".