0% found this document useful (0 votes)
95 views5 pages

VB Read Sheet Name

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.

Uploaded by

Paula Ce
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views5 pages

VB Read Sheet Name

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.

Uploaded by

Paula Ce
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

NESUG 17

Posters

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

NESUG 17

Posters

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

NESUG 17

Posters

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

NESUG 17

Posters

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

NESUG 17

Posters

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

You might also like