Using SAS Views and SQL Views Lynn Palmer, State of California, Richmond, CA



Similar documents
SAS Data Views: A Virtual View of Data John C. Boling, SAS Institute Inc., Cary, NC

Using Proc SQL and ODBC to Manage Data outside of SAS Jeff Magouirk, National Jewish Medical and Research Center, Denver, Colorado

CHAPTER 1 Overview of SAS/ACCESS Interface to Relational Databases

Technical Paper. Defining an ODBC Library in SAS 9.2 Management Console Using Microsoft Windows NT Authentication

SQL Pass-Through and the ODBC Interface

William E Benjamin Jr, Owl Computer Consultancy, LLC

SAS Client-Server Development: Through Thick and Thin and Version 8

Managing Tables in Microsoft SQL Server using SAS

9.1 SAS. SQL Query Window. User s Guide

Getting Your Data into SAS

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

9.1 SAS/ACCESS. Interface to SAP BW. User s Guide

ODBC Chapter,First Edition

Programming Tricks For Reducing Storage And Work Space Curtis A. Smith, Defense Contract Audit Agency, La Mirada, CA.

Using SAS With a SQL Server Database. M. Rita Thissen, Yan Chen Tang, Elizabeth Heath RTI International, RTP, NC

Paper FF-014. Tips for Moving to SAS Enterprise Guide on Unix Patricia Hettinger, Consultant, Oak Brook, IL

SAS 9.3 Drivers for ODBC

Simple Rules to Remember When Working with Indexes Kirk Paul Lafler, Software Intelligence Corporation, Spring Valley, California

System Requirements. SAS Profitability Management 2.2. Deployment

Encoding the Password

SAS Views The Best of Both Worlds

SAS Programming Tips, Tricks, and Techniques

Release 2.1 of SAS Add-In for Microsoft Office Bringing Microsoft PowerPoint into the Mix ABSTRACT INTRODUCTION Data Access

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

Meridian Digital Telephones

Effective Use of SQL in SAS Programming

SAS PASSTHRU to Microsoft SQL Server using ODBC Nina L. Werner, Madison, WI

DBF Chapter. Note to UNIX and OS/390 Users. Import/Export Facility CHAPTER 7

# or ## - how to reference SQL server temporary tables? Xiaoqiang Wang, CHERP, Pittsburgh, PA

Data Warehousing. Paper

Accessing a Microsoft SQL Server Database from SAS on Microsoft Windows

Chapter 2 The Data Table. Chapter Table of Contents

Using Version Control and Configuration Management in a SAS Data Warehouse Environment

Inmagic ODBC Driver 8.00 Installation and Upgrade Notes

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

The Query Builder: The Swiss Army Knife of SAS Enterprise Guide

Quick Start to Data Analysis with SAS Table of Contents. Chapter 1 Introduction 1. Chapter 2 SAS Programming Concepts 7

Meridian Digital Telephones

PharmaSUG Paper QT26

SAS 9.4 PC Files Server

Accessing Your Database with JMP 10 JMP Discovery Conference 2012 Brian Corcoran SAS Institute

Fun with PROC SQL Darryl Putnam, CACI Inc., Stevensville MD

UNIX Operating Environment

Define ODBC Database Library using Management Console

Tips for Constructing a Data Warehouse Part 2 Curtis A. Smith, Defense Contract Audit Agency, La Mirada, CA

Introduction to Proc SQL Steven First, Systems Seminar Consultants, Madison, WI

Managed Solution Center s TSM Managed Services FAQ

FileMaker 12. ODBC and JDBC Guide

SAS 9.2 Drivers for ODBC: User's Guide Second Edition

Accessing a Remote SAS Data Library. Transcript

Using the SQL Procedure

Big Data, Fast Processing Speeds Kevin McGowan SAS Solutions on Demand, Cary NC

SAS/ACCESS 9.3 Interface to PC Files

KEY FEATURES OF SOURCE CONTROL UTILITIES

ABSTRACT INTRODUCTION SAS AND EXCEL CAPABILITIES SAS AND EXCEL STRUCTURES

Analyzing the Server Log

NetIQ Identity Manager NetIQ Identity Manager Catalog Administrator

Lost in Space? Methodology for a Guided Drill-Through Analysis Out of the Wormhole

New Tricks for an Old Tool: Using Custom Formats for Data Validation and Program Efficiency

Paper TU_09. Proc SQL Tips and Techniques - How to get the most out of your queries

Asset Manager. Open Text Web Solutions Management Server 10.0

Katie Minten Ronk, Steve First, David Beam Systems Seminar Consultants, Inc., Madison, WI

WorkKeys Internet Version. Frequently Asked Questions

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

General Guide to Installation and Migration for Client/Server BAMS/DSS. Release 6.0

Writing Data with Excel Libname Engine

FileMaker 14. ODBC and JDBC Guide

Tips and Tricks SAGE ACCPAC INTELLIGENCE

Overview. NT Event Log. CHAPTER 8 Enhancements for SAS Users under Windows NT

ICE for Eclipse. Release 9.0.1

InfiniteInsight 6.5 sp4

Version Overview. Business value

Intelligent Query and Reporting against DB2. Jens Dahl Mikkelsen SAS Institute A/S

SQL Server An Overview

9.1 Supplement for SAS/ACCESS. Microsoft SQL Server. SAS/ACCESS for Relational Databases

USING SAS WITH ORACLE PRODUCTS FOR DATABASE MANAGEMENT AND REPORTING

Part No. N October CallPilot. Telephone Administration Guide

FileMaker 11. ODBC and JDBC Guide

Dynamic Decision-Making Web Services Using SAS Stored Processes and SAS Business Rules Manager

Switching from PC SAS to SAS Enterprise Guide Zhengxin (Cindy) Yang, inventiv Health Clinical, Princeton, NJ

User Guide. Analytics Desktop Document Number:

Foundations & Fundamentals. A PROC SQL Primer. Matt Taylor, Carolina Analytical Consulting, LLC, Charlotte, NC

Defining an OLEDB Library in SAS Management Console Using Windows Authentication

Technical Paper. Migrating a SAS Deployment to Microsoft Windows x64

Counting the Ways to Count in SAS. Imelda C. Go, South Carolina Department of Education, Columbia, SC

SAS Hints. data _null_; infile testit pad missover lrecl=3; input answer $3.; put answer=; run; May 30, 2008

for Networks Installation Guide for the application on a server September 2015 (GUIDE 2) Memory Booster version 1.3-N and later

for Networks Installation Guide for the application on the server July 2014 (GUIDE 2) Lucid Rapid Version 6.05-N and later

Importing TSM Data into Microsoft Excel using Microsoft Query

PaperClip Audit System Installation Guide

Configuring an Alternative Database for SAS Web Infrastructure Platform Services

Accessing Enterprise Data

Data Mining Commonly Used SQL Statements

When to Move a SAS File between Hosts

THE POWER OF PROC FORMAT

HansaWorld SQL Training Material

a presentation by Kirk Paul Lafler SAS Consultant, Author, and Trainer

IBM Cognos 8 Business Intelligence Analysis Discover the factors driving business performance

Release Bulletin Sybase ETL Small Business Edition 4.2

SAS Data Set Encryption Options

Transcription:

Using SAS Views and SQL Views Lynn Palmer, State of Califnia, Richmond, CA ABSTRACT Views are a way of simplifying access to your ganization s database while maintaining security. With new and easier ways to access data, me users are able to get their data directly from the ganization database. This data often requires some knowledge of the "rules" f summarizing and presenting the infmation. These rules may not be readily available to known by all staff. Additionally, database structures can be complex and difficult to navigate. One way to mange data access is to set up views that govern how the data is extracted. Views can, also, make it easier to access complex data sted in a database by summarizing the data f the user. You can create SAS views and SQL views that are transparent to your user and allow you to manage data access. INTRODUCTION What Are Views? A view is like a sted query. It contains no data, but instead contains code that describes, defines, selects sted data when it is needed. The data usually is in a database, however it could be in a SAS dataset file. Views are named and used like a table, dataset, file would be used. However, views are only code. The view is a subset superset of the data from which it is created. The advantage of using views is they take little space, they access the file database at execution reflecting the most current data, they can reference and join multiple sources of data reducing code, they can create tempary variables f statistics, they can be used to rename variables from the data source, and they can filter data making them good f security. Views can also be assigned passwds. Different views can access the same files f use by different groups of users. There are two groups of views presented in this paper. The first group is the views created and used in SAS code: SAS data views, PROC SQL views, in-line views, and interface (SAS/ACCESS descript) views. The second group of views is those created inside your database, frequently with SQL, with PROC SQL. These views are code residing inside the database. They can use the power of the database to process, update, and protect data. F many databases, your database administrat/creat must grant you permission to create these views create them f you. Why Use Views? Views allow f a "virtual tier", between the database tables and user code. This tier approach creates greater flexibility in your data system. Views can remain unchanged as system changes are made to the database, allowing the users to continue without disruption. Needed changes can be managed with views instead of changing the underlying database. Security can be adjusted inserted by creating different views f various users. In some data systems, the knowledge of how to operate in certain situations, what should be interpreted from a particular event, when to generate a special rept is known only to one two employees. By putting this knowledge into an ganizational library of views, me staff can act independently, and staff can change without crisis. VIEWS IN SAS CODE There are three types of SAS data views: DATA step views, PROC SQL views, and SAS/ACCESS views (view descripts). SAS data views are the member type, VIEW and are defined as native interface. Native views are the views created in a DATA step with PROC SQL. Interface views are created with SAS/ACCESS to read write data to a database (DBMS) such as DB2, ORACLE, ADABAS. From Version 7 fward, it has been possible to create native views that use the USING LIBNAME statement and therefe, are able to directly interface a database. Both native and interface views can be used with DATA PROC steps. SAS Data Views: SAS DATA step views tell SAS how to process data using the code that you specify in the DATA step. Only one view can be created in a DATA step and the view name must match one of the DATA step names. This type of view can only read the underlying data. DATA step views can generate datasets when the view is executed. A view is executed when it is used in a PROC DATA step. The view can be sted in a SAS library as a VIEW type, if you create it with a two-part name (library.viewname). The syntax is: DATA VIEWNAME / VIEW=LIBREF.VIEWNAME; To use a view, treat it like a dataset. Use the DATA= statement with PROC, and the SET statement with DATA steps. F example: DATA NEW; SET LIBREF.VIEWNAME; RUN; PROC FREQ DATA=LIBREF.VIEWNAME; There are security features that can be used when creating a SAS DATA view. You can passwd your views. There is me on using passwds under the passwd section of this paper. The phrase (SOURCE=ENCRYPT) after the view name will encrypt your view, f example: DATA VIEW_A/VIEW=LIB.VIEW_A(SOURCE=ENCRYPT); The term, DESCRIBE, can be used to write a copy of the view source to your SAS log: DATA AA; SET VIEWNAME; DESCRIBE; RUN; Printing the source code to log will allow you to see the name of the underlying data file database, and how the code in the view is a subset a superset of that data.

Using DATA step views does reduce the efficiency of the program. Therefe, code using a view may run a little slower than code accessing the data source directly. This reduction may be reasonable, if you are realizing a saving of time by reusing code. Views can be used to reduce wkspace, because SAS does not read the underlying data into a wk dataset when a view is used. Data in large files can be selected into a subset using a view preventing the job from running out of space and failing. Wking on an IBM mainframe, I use tempary DATA step views to select the data from tape files of 500,000 to million recds. The code looks like this: DATA VIEWNAME/VIEW=VIEWNAME; INFILE DDNAME; INPUT @1 VAR1 $CHAR1. me input ; IF VAR1 LE 9 AND VAR1 GT 0; RUN; PROC FREQ DATA=VIEWNAME; TABLES me code ; This use allows me wkspace f processing. The view is not saved, once the program ends and the wkspace is deleted the view is gone. The drawbacks are that you can t use a DATA view with PROC SORT without creating a dataset and if you are running many PROC statements using the view, your efficiency declines. SQL Views: SQL views are defined in PROC SQL using a CREATE VIEW statement. They are based on SAS datasets, sequential files, databases, and other views. They can contain code to connect to a database using the PROC SQL pass-through facility. They can be in the program code sted in a SAS library. These views are me efficient because at execution they utilize the SQL Process that optimizes the access strategy. The following is an example: CREATE VIEW AS LIBNAME.VIEWNAME; FROM LIBNAME.TABLE WHERE SOME_CONDITION IS TRUE; Views contain no data so they take little space to ste. They are sted in a SAS library as a VIEW member. You can refer to tables (datasets) and views using a view which resides in the same SAS library without the library name reference. The view is referenced in your SAS code like a dataset in the FROM clause like a table. F example: PROC FREQ DATA=LIBNAME.VIEWNAME; CREATE TABLE AS MYTABLE; FROM LIBNAME.VIEWNAME; Beginning in Version 7, a PROC SQL view can be used to update the underlying database data. To update, the view must be based only on one database table a database view with no calculated fields based on the table. You can use the ORDER BY clause in your CREATE VIEW statement, however it may be me efficient to st when needed when using a view. Connect to/connection to: One of the imptant features of SQL views is that they can be used to connect to other software vend s products, such as DB2, ORACLE, MSAccess, MSExcel, MSSQLServer. The CONNECTION TO, and CONNECT TO statements allow this access. You can place these statements in a view so that your users can access the database without struggling with the code. The following is code f connecting to DB2: CONNECT TO DB2 AS MYCON (SSID=YOURID) CREATE VIEW AS LIBNAME.VIEWNAME FROM LIBNAME.TABLE WHERE SOME_CONDITION IS TRUE; ODBC ODBC stands f Open Database Connectivity. It is an interface standard that provides a common application programming interface (API) f accessing databases. Many software products that run in the Windows operating environment adhere to this standard, giving users access to data that was created with other software. To create a ODBC link on your PC, go to settings, control panel, and ODBC data sources. There you will find a set of Microsoft menus to guide you in making a link to your database. Use that ODBC link in the DSN= ption of your CONNECT TO statement. Your SAS code will look like this: CONNECT TO ODBC AS MYCON (DSN = MYLINK ); CREATE VIEW MYVIEW AS ( FROM TABLE T1 WHERE T1.APP_DATE BETWEEN '02/01/2003' AND '02/08/2003'); In-Line Views: In-line views are defined in a PROC SQL query and used as part of the query. These views select manipulate data in your program f use by that program. Using these views can simplify complex code. F example, in the following program, I am only selecting recds from Table2 that are true f some condition in Table1, instead of using one program to select the true recds from Table 1 and using another program to search f those recds in Table 2. CREATE TABLE AS MYTABLE FROM (SELECT TABLE1.FIELD FROM LIBREF.TABLE1 WHERE SOME_CONDITION = TRUE;) MYVIEW, LIBREF.TABLE2 WHERE MYVIEW.FIELD = TABLE2.FIELD; In this code, SQL is using the view, MYVIEW, described in the FROM statement in parentheses with a table, LIBREF.TABLE1, from the database. The in-line view must be assigned a name after the parentheses. This in-line view demonstrates me efficient programming by reducing the number of steps need to select data and link.

Libname A LIBNAME statement with a libref describing the path to your database will allow querying, updating, deleting data in your database. The LIBNAME statement can be embedded in your PROC SQL view. Additionally, the SAS/ACCESS view libref can be used with LIBNAME automatically connecting you to your database. This use of LIBNAME is new with version 7. The USING statement containing the LIBNAME statement gives this flexibility. The libref that is assigned inside the view is local to the view and will not conflict with other uses of the same libref in the same SAS session. The libref is de-assigned at the end of the query. CREATE VIEW MYVIEW AS FROM MYLIB.TABLE TABLE USING LIBNAME MYLIB DB2; Multiple LIBNAME statements can be specified, separated by commas. And the USING statement must be the last one in your SELECT statement. In the following example, a connection is made and the libref, MYREF, is assigned to an ORACLE database. CREATE VIEW MYLIB.VIEW1 AS FROM MYREF.TABLE TABLE USING LIBNAME MYREF ORACLE USER=USERNAME PASS=PASSWORD PATH= PATH TO THE DATABASE/URL ; {PATH= C:/MYDIRECTORY/MYDATABASE ;} Access Descript Interface Views: SAS/ACCESS views are views that interface with the data of other software products. A descript is created f your database table file using the SAS/ACCESS product f your non-sas database files. These descripts can only access one table file at a time. The view is based on this descript. The descript contains the appropriate engine f the database files that you are accessing with the view. In these views, you can fmat fields with SAS fmats. You can assign different names to the database fields in the view and change the field length. You can limit the fields available to your users and you can include SAS code to change field values create new fields from the fields in the underlying database table. Additionally, with this type of view, you can change the data in the underlying database table by updating, modifying, inserting deleting. The SAS/ACCESS descript f the table can be created interactively in batch. The following code will create a descript and its view f a MSExcel wksheet: PROC ACCESS DBMS=XLS; CREATE LIBREF.TABLE.ACCESS; PATH= C:\YOURFILE.XLS ; GETNAMES=YES; LIST=ALL; CREATE LIBREF.TABLE.VIEW; SELECT FIELD; SUBSET WHERE SOME_CONDITION = TRUE; LIST VIEW; RUN; If you are going to access a view repeatedly, you can improve perfmance by extracting the infmation you need into a SAS dataset. By using SET with the DATA step OUT= with a PROC step, a SAS dataset can be created from the view. Repeated referencing of a view in the same SAS program/session will lead to use of me resources than necessary to get the job done. Passwds Views can have passwds set when they are created which limit their use. Passwd protecting a view can affect the actual view descript as well as the underlying data. There are three levels of security. The first is READ which allows read only of underlying data, allows source code of view to be printed to the log using DESCRIBE, and allows replacement of the view (overwriting). The second is WRITE allows writing to data/database. The third is ALTER which protects against reading, protects against source code being printed to the log, and protects against replacement.? To assign a passwd, you use the key wds READ, WRITE ALTER and an equals sign with your passwd in parentheses. F example: CREATE VIEW MYLIB.MYVIEW(WRITE=MYPASS) AS DATA MYVIEW/VIEW=MYVIEW(ALTER=MYPASS); To use the view, the passwd must be used. It is given after the view name: DATA NEW; SET VIEW(PW=MYPASS); Passwds can add additional security beyond the security offered by creating different views f different groups of users. SQL VIEWS DB2 Views: Views in DB2 are SQL code. They can be created using PROC SQL, if you are granted permission to create views in your ganization's database. The SQL features available depend on the database not SAS. These views can be very powerful and efficient as they are part of the database and can utilize the database Processing tools. Code to produce a DB2 view is as follows: CONNECT TO DB2 AS MYCON (SSID = YOURID); CREATE VIEW AS FULL.NAME.MYVIEW FROM FULL.TABLE.NAME TABLE WHERE SOME_CONDITION = TRUE; These views can also be programmed in SQL executed by

the database administrat. The view will be sted in the database like the tables in the database. However, unlike the tables, the views will not require space. MSAccess Queries: You can set up MSAccess queries to act like views in a MSAccess database. The query will contain SQL code (Microsoft 's version). You can access this query the same way that you access a table using the PROC SQL pass through facility (CONNECT TO) and ODBC. These queries can be created visually using MSAccess menus by programming SQL code in MSAccess. The following code will allow you to use a query in MSAccess with SAS. You will first need to set up the ODBC connection (MYLINK) on your PC (see above). CONNECT TO ODBC AS MYCON (DSN= MYLINK ); CREATE TABLE MYTABLE AS (SELECT QUERYNAME.FIELD FROM QUERYNAME); MSSQLServer Views: SQL Server views are very similar to MSAccess queries. They can be connected to with an ODBC connection. The user uses the view like a table in the database. These views can change data in the underlying database. They allow f the processing to be done inside the database using the database which provides greater efficiency. A view in one SQLServer database can access another SQLServer database. CREATE VIEW dbo.view_1 AS SELECT VAR1, VAR_DT AS SYS_DATE, VAR2 FROM SERVERNAME.SYSNAME.dbo.TABLE WHERE (VAR1 LIKE '%ABC%' VAR1 LIKE '%CDE%') AND (VAR2 = 'Some character string'); In our office, the are production databases and study databases. Frequently, they need to be used together. We were using two PROC SQL CONNECTION TO statements to access our data in different databases. Linking the two resulting tables was required. By placing a view of the production database that limits the fields and the time period into the study database, users are able to access both databases with one connection. Data retrieval is faster. This is an example of code similar to the connecting SQL view in the study database: CREATE VIEW VIEW_2 AS FROM DATA_1.dbo.VIEW_1 WHERE SYS_DATE > 1/6/2002 ; Such a simple view reduced complaints, and errs dramatically. EXAMPLES OF VIEWS Views as an excellent tool to build a virtual layer between your database and your users. An example is the use of DB2 views in our research unit. The administrat of our DB2 database named the fields and tables in the database with 16 character names to reflect the source names from another database. Our unit writes SAS code to retrieve data from DB2. We shares our SAS code within the unit. We like standardized names f fields and we needed eight character names f fields and tables befe Version 7. So, we developed SAS names f the fields, grouped in ways that make analysis easier. We, then, created views with eight character names in DB2 that renamed and regrouped the fields. These views don't take up stage space, they allows us to find fields faster, and they run as fast as using the underlying tables without the views. Our DB2 database administrat changed the structure of the underlying tables in our database, combining several together f me efficiency. He recreated the views by changing only the table names in the code of the views, so the views appear to be unchanged to the users. This use of views allowed him to make the changes without having to disturb our method of wking causing our code to be rewritten. Thereby, avoiding a battle over how and where fields are grouped and named. In our data, one client can have many recds. Which recd to analyze can become difficult to determine without detailed coding. A view sted in a SAS library with the indicat pointing to the crect recd to choose f analysis allows analysts to avoid coding and errs when doing client analysis. By using this view, NEW, the dataset, OLD is checked f missing dates and those recds with errs are filtered out of the NEW dataset. At the same time, a dataset of the recds with missing dates is created, ERRORS. DATA NEW ERRORS / VIEW = NEW; SET OLD; IF DATE =. THEN OUTPUT ERRORS; ELSE OUPUT NEW; RUN; The dataset, ERRORS is created only when the view is executed and there are missing dates. CONCLUSION The examples above are simple, but the application of views can be as complex as your situation needs. Views can be a powerful tool when accessing data in your database. Views can allow your users to proceed without misusing the data in your database. REFERENCES SAS On-Line Documentation Version8, SAS Institute Inc., Cary, NC, USA, 1999. SAS Language, Reference, Version 6, First Edition, SAS Institute Inc., Cary, NC, USA, 1990. SAS PROCedures Guide, Version 6, Third Edition, SAS Institute Inc., Cary, NC, USA, 1990. SAS Guide to the SQL PROCedure, Usage and Reference, Version 6, First Edition, SAS Institute Inc., Cary, NC, USA, 1989. SAS/ACCESS Interface to IMSDL/I:Reference;

DATA; LIBNAME, SAS/ACCESS; The SQL PROCedure; SAS On-line Documentation Version 8, SAS Institute Inc., Cary, NC, USA, 1999 Lather, Kirk Paul, Frame Your View of Data with the SQL PROCedure, PROCeedings of the Third Annual Western Users of SAS Software Regional Users Group Conference, 1995, Pages 333-336. Lather, Kirk Paul, Querying the Data Warehouse with the SQL PROCedure SELECT Statement, PROCeedings of the Sixth Annual Western Users of SAS Software Regional Users Group Conference, 1998, Pages 302-306. Wilson, Steven A., Using SAS Views f Data Source Visualization, PROCeedings of the Fourth Annual Western Users of SAS Software Regional Users Group Conference, 1996, Pages 389-398. Kuligowski, Andrew T., An Overview of Techniques to Introduce External Data Into the SAS System, PROCeedings of the Sixth Annual Western Users of SAS Software Regional Users Group Conference, 1998, Pages 491-509. SAS is a registered trademark trademark of SAS Institute Inc. in the USA and other countries. indicates USA registration. IBM is a registered trademark trademark of International Business Machines Cpation in the USA and other countries. indicates USA registration. MSWindows, MSDOS, MSVisual Basic, MSWd and MSExcel, MSSQLServer are products of the Microsoft Cpation. Copyright 1983-2002 Microsoft Cpation. All rights reserved. Other brand and product names are registered trademarks trademarks of their respective companies. AUTHOR Lynn Palmer, State of Califnia, DHS, GDB 850 Marina Bay Pkwy, F-175 Richmond, CA 94804-6403 Email: lpalmer@dhs.ca.gov, lynnpalmer@comcast.net Phone: (510) 412-1510.