VBA Microsoft Access 2007 Macros to Import Formats and Labels to SAS



Similar documents
Applications Development

A Comparison of SAS versus Microsoft Excel and Access s Inbuilt VBA Functionality

A Comparison of SAS versus Microsoft Excel and Access s Inbuilt VBA Functionality Jozef Tarrant, Amadeus Software Ltd., Oxford, UK

Connecting to an Excel Workbook with ADO

Managing Tables in Microsoft SQL Server using SAS

A Microsoft Access Based System, Using SAS as a Background Number Cruncher David Kiasi, Applications Alternatives, Upper Marlboro, MD

Abstract. Introduction. System Requirement. GUI Design. Paper AD

From Database to your Desktop: How to almost completely automate reports in SAS, with the power of Proc SQL

Importing Excel File using Microsoft Access in SAS Ajay Gupta, PPD Inc, Morrisville, NC

IENG2004 Industrial Database and Systems Design. Microsoft Access I. What is Microsoft Access? Architecture of Microsoft Access

Changing the Display Frequency During Scanning Within an ImageControls 3 Application

Access Tutorial 14: Data Access Objects

A Method for Cleaning Clinical Trial Analysis Data Sets

REx: An Automated System for Extracting Clinical Trial Data from Oracle to SAS

Visual Basic Programming. An Introduction

THE HELLO WORLD PROJECT

Combining SAS LIBNAME and VBA Macro to Import Excel file in an Intriguing, Efficient way Ajay Gupta, PPD Inc, Morrisville, NC

Importing Excel Files Into SAS Using DDE Curtis A. Smith, Defense Contract Audit Agency, La Mirada, CA

Remove Orphan Claims and Third party Claims for Insurance Data Qiling Shi, NCI Information Systems, Inc., Nashville, Tennessee

Add an Audit Trail to your Access Database

Using Macros to Automate SAS Processing Kari Richardson, SAS Institute, Cary, NC Eric Rossland, SAS Institute, Dallas, TX

Creating Dynamic Reports Using Data Exchange to Excel

PharmaSUG Paper PO01

Introduction. Why (GIS) Programming? Streamline routine/repetitive procedures Implement new algorithms Customize user applications

Analyzing the Server Log

ABSTRACT THE ISSUE AT HAND THE RECIPE FOR BUILDING THE SYSTEM THE TEAM REQUIREMENTS. Paper DM

Managing very large EXCEL files using the XLS engine John H. Adams, Boehringer Ingelheim Pharmaceutical, Inc., Ridgefield, CT

Siemens Applied Automation Page 1 11/26/03 9:57 PM. Maxum ODBC 3.11

Macros allow you to integrate existing Excel reports with a new information system

Improving Your Relationship with SAS Enterprise Guide

Order from Chaos: Using the Power of SAS to Transform Audit Trail Data Yun Mai, Susan Myers, Nanthini Ganapathi, Vorapranee Wickelgren

CDW DATA QUALITY INITIATIVE

A Macro to Create Data Definition Documents

You have got SASMAIL!

Data Presentation. Paper Using SAS Macros to Create Automated Excel Reports Containing Tables, Charts and Graphs

Hands-on Exercise 1: VBA Coding Basics

Using Pharmacovigilance Reporting System to Generate Ad-hoc Reports

Experiences in Using Academic Data for BI Dashboard Development

Interfacing SAS Software, Excel, and the Intranet without SAS/Intrnet TM Software or SAS Software for the Personal Computer

Building a Marketing Dashboard using Excel and SAS. Tim Walters InfoTech Marketing

ing Automated Notification of Errors in a Batch SAS Program Julie Kilburn, City of Hope, Duarte, CA Rebecca Ottesen, City of Hope, Duarte, CA

Debugging Complex Macros

How to Use SDTM Definition and ADaM Specifications Documents. to Facilitate SAS Programming

Paper PO03. A Case of Online Data Processing and Statistical Analysis via SAS/IntrNet. Sijian Zhang University of Alabama at Birmingham

Database Automation using VBA

Spelling Checker Utility in SAS using VBA Macro and SAS Functions Ajay Gupta, PPD, Morrisville, NC

Search and Replace in SAS Data Sets thru GUI

- SAP BusinessObjects and Xcelsius articles, links and ressources

Introduction to PASW Statistics

KEYWORDS ARRAY statement, DO loop, temporary arrays, MERGE statement, Hash Objects, Big Data, Brute force Techniques, PROC PHREG

Paper An Introduction to SAS PROC SQL Timothy J Harrington, Venturi Partners Consulting, Waukegan, Illinois

Get in Control! Configuration Management for SAS Projects John Quarantillo, Westat, Rockville, MD

Access to Relational Databases Using SAS. Frederick Pratter, Destiny Corp.

Automate Data Integration Processes for Pharmaceutical Data Warehouse

AN ANIMATED GUIDE: SENDING SAS FILE TO EXCEL

Microsoft Access 2010 Part 1: Introduction to Access

TS2Mascot. Introduction. System Requirements. Installation. Interactive Use

PharmaSUG 2014 Paper CC23. Need to Review or Deliver Outputs on a Rolling Basis? Just Apply the Filter! Tom Santopoli, Accenture, Berwyn, PA

SUGI 29 Applications Development

Tales from the Help Desk 3: More Solutions for Simple SAS Mistakes Bruce Gilsen, Federal Reserve Board

1. Create SQL Database in Visual Studio

SAS, Excel, and the Intranet

Hummingbird Enterprise

Leveraging the SAS Open Metadata Architecture Ray Helm & Yolanda Howard, University of Kansas, Lawrence, KS

Using Microsoft Excel for Data Presentation Peter Godard and Cyndi Williamson, SRI International, Menlo Park, CA

How-To Guide. Crystal Report Demo. Copyright Topaz Systems Inc. All rights reserved.

Effective Use of SQL in SAS Programming

Table Lookups: From IF-THEN to Key-Indexing

An macro: Exploring metadata EG and user credentials in Linux to automate notifications Jason Baucom, Ateb Inc.

SAS Rule-Based Codebook Generation for Exploratory Data Analysis Ross Bettinger, Senior Analytical Consultant, Seattle, WA

Essential Project Management Reports in Clinical Development Nalin Tikoo, BioMarin Pharmaceutical Inc., Novato, CA

Programming in Access VBA

Create an Excel report using SAS : A comparison of the different techniques

Getting Started with STATISTICA Enterprise Programming

Tricks Using SAS Add-In for Microsoft Office

Product: DQ Order Manager Release Notes

Methodologies for Converting Microsoft Excel Spreadsheets to SAS datasets

CAPIX Job Scheduler User Guide

StARScope: A Web-based SAS Prototype for Clinical Data Visualization

SUGI 29 Coders' Corner

Using SAS Enterprise Business Intelligence to Automate a Manual Process: A Case Study Erik S. Larsen, Independent Consultant, Charleston, SC

The FTS Interactive Trader lets you create program trading strategies, as follows:

We begin by defining a few user-supplied parameters, to make the code transferable between various projects.

SAS Credit Scoring for Banking 4.3

Microsoft' Excel & Access Integration

Creating a Windows Service using SAS 9 and VB.NET David Bosak, COMSYS, Kalamazoo, MI

National Database System (NDS-32) Macro Programming Standards For Microsoft Word Annex - 8

PharmaSUG Paper AD11

Imelda C. Go, South Carolina Department of Education, Columbia, SC

PharmaSUG Paper QT26

Transcription:

WUSS 2011 VBA Microsoft Access 2007 Macros to Import Formats and Labels to SAS Maria S. Melguizo Castro, Jerry R Stalnaker, and Christopher J. Swearingen Biostatistics Program, Department of Pediatrics University of Arkansas for Medical Sciences, Little Rock, AR ABSTRACT It is a common practice for statisticians and data analysts to import data from Microsoft Access into SAS for analysis. Although it is relatively straightforward to transfer data between the two programs, currently there is not an option to fully transfer descriptions or data formats into SAS. This paper proposes a solution using both Visual Basic for Applications (VBA) and SAS macros to address this issue and create a more complete SAS dataset with descriptions as variable labels and with appropriate data formats. KEYWORDS: Data integration, Macros INTRODUCTION Microsoft (MS) Access 2007 description of field names in the database tables are usually employed when collecting data. These descriptions inherently provide a statistician or data analyst with meaningful labels to be used for SAS dataset variables. However, SAS v9.2 does not have the capability of importing descriptions from MS Access. Additionally, lookup fields are sometimes employed to create formats for the variables (e.g., 0=Male, 1=Female); there is also not an option in SAS to import those. To convert variable labels and value formats from MS Access to SAS, a Visual Basic for Applications (VBA) macro was created in MS Access that generates a metadata table consisting of field names, descriptions, and table names stored within the database. This new table is subsequently imported into SAS and used to create labels for variables in SAS datasets. In order to convert the variable formats, VBA macro converted MS Access variable formats stored in field lists into a complete PROC FORMAT statement exported as a text file for implementation into SAS. The process is illustrated with an example database. ACCESS DATABASE An example of a MS Access database is presented below to mimic a data collection instrument typical of a research study. The database contains three tables: Demographics, Test and Questionnaire. Figure 1 presents the relationships between the tables. In this example, table Demographics captures unique study participant data (i.e. one row represents one unique record); unique identifier is linked to both tables Test and Questionnaire in a 1-to-Many relationship (i.e. multiple observations are collected on each unique study participant). Inside a Design View of any table, variable descriptions can be located for as well as field lists defined for variable formats (Figure 2). 1

Figure 1. Example Database and Relationships ACCESS VBA FOR VARIABLE LABELS In the Visual Basic window from MS Access, a new Module was inserted to create a VBA macro. The following code creates a new table in the MS Access database that contains all of the field names, descriptions and names for all the tables in the database. This code should be run first before invoking the SAS code. Running the macro CreateLabels generates the metadata table of variable labels; the macro calls the CreateTable code as a sub routine to initialize the metadata table. This VBA code can be used in any database. Please note that any database that contains a table Descriptions may need to be renamed as the VBA macro would delete such table. 2

Figure 2. Example of Lookup fields ' Auxiliary sub routine CreateTable Sub CreateTable() Dim wspdefault As Workspace, dbs As Database Dim tdf As TableDef, fld1 As Field, fld2 As Field Dim idx As Index, fldindex As Field End Sub Set wspdefault = DBEngine.Workspaces(0) ' Open Current database. Set dbs = CurrentDb() ' Create new table with three fields. Set tdf = dbs.createtabledef("descriptions") Set fld1 = tdf.createfield("variablename", dbtext) Set fld2 = tdf.createfield("variabledescription", dbtext, 50) Set fld3 = tdf.createfield("tblname", dbtext) ' Append fields. tdf.fields.append fld1 tdf.fields.append fld2 tdf.fields.append fld3 ' Append TableDef object. dbs.tabledefs.append tdf dbs.tabledefs.refresh Set dbs = Nothing 3

' Main VBA subroutine Sub CreateLabels() Dim tdf As TableDef Dim db As Database Dim fld As Field Dim prp As Property Dim pos As Integer Dim repl, replwith As String Dim repldesc, repldescwith As String Dim tname As String Dim rst As Recordset Dim i As Integer Dim SQL As String Dim DescrAvailable As Boolean Set db = CurrentDb() 'If the table descriptions exist then delete it For i = 0 To db.tabledefs.count - 1 If db.tabledefs(i).name = "Descriptions" Then db.tabledefs.delete "Descriptions" db.tabledefs.refresh Exit For 'the macro CreateTable will create a table with the Descriptions Call CreateTable Set rst = db.openrecordset("descriptions") For Each tdf In db.tabledefs For Each fld In tdf.fields DescrAvailable = False For Each prp In fld.properties If prp.name = "Description" Then With rst.addnew!variablename = fld.name!variabledescription = prp.value!tblname = tdf.name.update DescrAvailable = True End With If DescrAvailable = False Then With rst.addnew!variablename = fld.name!tblname = tdf.name.update End With DescrAvailable = False db.close End Sub 4

SAS V9.2 CODE FOR VARIABLE LABELS The first part of the SAS code was created to import the three data tables available in the database, as well as the Description table. The second part of the code takes the Description table and use it to create labels into the three newly created SAS data sets. To make this code useful for any specific data analysis, the analyst would need to modify the database path, and create as many %ImportTables(), and %Labels() macro invocations as tables in the database. /*************************************/ /* Import access tables*/ /*************************************/ %Macro ImportTables(TableName); PROC IMPORT OUT= WORK.&TableName DATATABLE= "&TableName" DBMS=ACCESS REPLACE; DATABASE="C:\SUGI\DatabaseSUGI.accdb"; SCANMEMO=YES; USEDATE=NO; SCANTIME=YES; RUN; %mend; %ImportTables(Demographics); %ImportTables(Test); %ImportTables(Questionnaire); %ImportTables(Descriptions); /***************************************************/ /* Macro to Create Labels using table Descriptions */ /***************************************************/ %macro Labels(TableName); proc sql; select count(*) into:numvar from Descriptions where tblname="&tablename"; %let numvar =&numvar; select VariableDescription into :varlab1-:varlab&numvar from Descriptions where tblname="&tablename"; select VariableName into :varname1-:varname&numvar from Descriptions where tblname="&tablename"; Data &TableName; set &TableName; %do i=1 %to &numvar; label &&varname&i="&&varlab&i"; %end; run;quit; %mend; %Labels(Demographics); %Labels(Test); %Labels(Questionnaire); 5

ACCESS VBA FOR VARIABLE FORMATS Capturing the field values for variables within the database is completed using the VBA macro createfields, which in turn calls two sub-functions CreateTableFormats and CreateFormats. This macro creates a table Format in the database containing all of the field labels and corresponding values for each variable described with a field list. The final VBA macro CreateSASFormat converts the data within table Format into a SAS PROC FORMAT chunk and exports the code chunk as a text file. This text file can then be utilized by an analyst in SAS. Please note that the current location for the text file is a simple directory listing to a user s local hard drive (i.e. C:\). '--------------------------------------------------------------------- ' Code to create formats '--------------------------------------------------------------------- Sub CreateTableFormats() Dim wspdefault As Workspace, dbs As Database Dim tdf As TableDef, fld1 As Field, fld2 As Field, fld3 As Field, fld4 As Field Dim idx As Index, fldindex As Field Set wspdefault = DBEngine.Workspaces(0) ' Open Current database. Set dbs = CurrentDb() ' Create new table with four fields. Set tdf = dbs.createtabledef("format") Set fld1 = tdf.createfield("variablename", dbtext) Set fld2 = tdf.createfield("variablenumericvalue", dbtext, 150) Set fld3 = tdf.createfield("variableformat", dbtext) Set fld4 = tdf.createfield("tblname", dbtext) End Sub ' Append fields. tdf.fields.append fld1 tdf.fields.append fld2 tdf.fields.append fld3 tdf.fields.append fld4 ' Append TableDef object. dbs.tabledefs.append tdf dbs.tabledefs.refresh Set dbs = Nothing 6

Sub CreateFormats() Dim tdf As TableDef Dim db As Database Dim fld As Field Dim prp As Property Dim pos As Integer Dim repl, replwith As String Dim repldesc, repldescwith As String Dim tname As String Dim rst As Recordset Dim i As Integer Dim SQL As String Dim DescrAvailable As Boolean Dim strnname Set db = CurrentDb() ' If the Format exist then delete it For i = 0 To db.tabledefs.count - 1 If db.tabledefs(i).name = "Format" Then db.tabledefs.delete "Format" db.tabledefs.refresh Exit For 'the macro CreateTable will create a table with the Formats Call CreateTableFormats Set rst = db.openrecordset("format") For Each tdf In db.tabledefs For Each fld In tdf.fields For Each prp In fld.properties If prp.name = "RowSource" Then strname = prp.value fldname = fld.name tdfname = tdf.name db.close End Sub Call createfields(strname, tdfname, fldname) 7

Public Function createfields(strname, tdfname, fldname) Dim rst As Recordset Dim db As Database Set db = CurrentDb() Set rst = db.openrecordset("format") pos1 = 1 i = 1 Do While (InStr(i, strname, ";") <> 0) For j = 1 To 2 i = InStr(i, strname, ";") pos2 = i If pos1 < pos2 Then If j = 1 Then newstr1 = Mid(strname, pos1, (pos2 - pos1)) Else If j = 2 And pos1 < pos2 Then newstr2 = Mid(strname, pos1, (pos2 - pos1)) With rst.addnew!variablename = fldname!variablenumericvalue = newstr1!variableformat = newstr2!tblname = tdfname.update End With Else newstr2 = Mid(strname, pos1, Len(strname) - 1) With rst.addnew!variablename = fldname!variablenumericvalue = newstr1!variableformat = newstr2!tblname = tdfname.update End With Exit Function i = i + 1 pos1 = i j Loop End Function 8

Public Sub CreateSASFormat() Dim rst As Recordset Dim db As Database Set db = CurrentDb() Set rst = db.openrecordset("format") Dim countvariables As Integer Dim SQL As String Dim VarNames() As String Dim VarNumeric() As Integer Dim VarFormat() As String Dim fs, a As Object n = rst.recordcount ReDim VarNumeric(n) As Integer ReDim VarFormat(n) As String ReDim VarNames(n) As String rst.movefirst Varname = rst.fields("variablename") VarNames(1) = rst.fields("variablename") VarNumeric(1) = rst.fields("variablenumericvalue") VarFormat(1) = rst.fields("variableformat") countvariables = 1 rst.move For i = 2 To rst.recordcount If rst.fields("variablename") <> Varname Then countvariables = countvariables + 1 VarNames(countVariables) = rst.fields("variablename") Varname = rst.fields("variablename") VarNumeric(i) = rst.fields("variablenumericvalue") VarFormat(i) = rst.fields("variableformat") rst.move rst.movefirst Varname = rst.fields("variablename") SQL = "Proc Format " jini = 1 For i = 1 To countvariables SQL = SQL + ";" + Chr$(10) + " value " + VarNames(i) + " " For j = jini To rst.recordcount If rst.fields("variablename") = Varname Then SQL = SQL & rst.fields("variablenumericvalue") & "= '" & rst.fields("variableformat") & "' " Varname = rst.fields("variablename") rst.move Else jini = j Varname = rst.fields("variablename") j = rst.recordcount Debug.Print SQL 9

SQL = SQL + ";" + Chr$(10) + "run ;" Set fs = CreateObject("Scripting.FileSystemObject") Set a = fs.createtextfile("c:\sasformats.txt", True) a.writeline SQL a.close End Sub CONCLUSION It is a common practice between statisticians and data managers to import data collected in Access into SAS for analysis. The current paper shows that the capabilities of SAS can be expanded by the use of VBA macros. REFERENCES Melguizo Castro MS, Stalnaker JR, and Swearingen CJ. Using Microsoft Access 2007 description of field names in tables as variable labels in SAS datasets. SAS Global Forum Proceedings 2011; Paper 236:1-5. Roper, Christopher A.. Using SAS and DDE to execute VBA macros in Microsoft Excel. Qualex Consulting Services, Inc., Cary, NC. USA. Available at http://www2.sas.com/proceedings/sugi25/25/cc/25p098.pdf Lu, Zaizai; Shen, David. Data Transfer from Microsoft Access to SAS Made Easy. AstraZeneca Pharmaceutical and, ClinForce Inc. Available at http://www.lexjansen.com/pharmasug/2005/coderscorner/cc12.pdf Bessler, LeRoy. SAS -with-excel Application Development: Tools and Techniques., Assurant Health, Milwaukee, Wisconsin, USA. Available at http://www2.sas.com/proceedings/sugi31/022-31.pdf Nan, Rubin; Mullins, David. A Microsoft Access GUI for SAS Automation. PRA International, Lenexa, KS. Available at http://www2.sas.com/proceedings/sugi30/044-30.pdf Luo, Lian; Yiin, James Ph.D. A VB.NET utility to transfer multiple data files between SAS and Microsoft Access/Excel by automatically generating SAS Import/Export statements. SRA International Inc, Cincinnati, OH. National Institute for Occupational Safety and Health, Cincinnati, OH. Available at http://support.sas.com/resources/papers/proceedings10/191-2010.pdf Shvorob, Dimitri. I Want It All - Recovering Variable Labels When Importing Data from Microsoft Acces. Vanderbilt University, Nashville, TN. Available at: http://www2.sas.com/proceedings/forum2007/003-2007.pdf CONTACT INFORMATION Your comments and questions are valued and encouraged. Contact the author at: Name: Maria S. Melguizo Castro Enterprise: University of Arkansas For Medical Sciences UAMS - Department of Pediatrics Address: 1 Childrens Way, Slot 512-43 City, State ZIP: Little Rock, AR 72202-3500 Work Phone: (501) 364-6619 Fax: (501)364-1431 E-mail: msmelguizo@uams.edu Web: http://www.arpediatrics.org/research/biostatistics 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. Other brand and product names are trademarks of their respective companies. 10