Accessing DBMS Data with SAS/ACCESS

A Brief Overview

The SAS/ACCESS interface engine is a tool that enables you to transfer data between a database management system (DBMS) and SAS. SAS can access a variety of DBMS, such as Teradata, Oracle, SQL Server, Greenplum, and so on. This particular interface engine has transparent Read and Write access capabilities, so you might be reading Teradata data and not even realize it.
SAS/ACCESS offers interface engines that enable you to read third-party data. For example, if you need to read data in DB2, there is a SAS/ACCESS Interface engine to DB2. If you need to read Oracle, there is a SAS/ACCESS Interface to Oracle, and an Oracle engine.
Figure 6.1 SAS/ACCESS Technology
SAS/ACCESS Technology
When using SAS and working with a DBMS, you must take into account the data transfer between the two systems.
The best way to have more efficient code is first to maximize the amount of processing that occurs on the DBMS. Then bring the data back to SAS for more advanced data manipulation, analytics, or visualizations.
There are two ways to connect to a database in SAS:
  • SQL pass-through facility
  • SAS/ACCESS LIBNAME statement
In the SQL pass-through facility, you can pass the SQL statements directly to the database.
In most instances, the SAS/ACCESS LIBNAME statement enables you to use a library reference that you assign to your relational database. It enables you to reference a database object directly within your SAS code. For example, where you would traditionally use a SAS data set name in your DATA step, you can use the name of your database table in the DATA step, as well as any of the SAS procedures. There are many options such as the SQL pass-through facility and the SAS/ACCESS LIBNAME engine that control how you connect directly to the database.
When you are reading data from a specific database, each SAS/ACCESS LIBNAME statement provides an engine name as part of the LIBNAME statement syntax.

SQL Pass-Through Facility

A Brief Overview

Suppose you are an Oracle SQL analyst, and you want to pull data from your Oracle database into SAS. You can use the SQL pass-through facility to send DBMS-specific SQL statements directly to Oracle for execution. The syntax executes as if you are coding inside Oracle, and processing occurs within Oracle. Once the Oracle query completes execution, the data is sent to SAS for processing.
Figure 6.2 SQL Pass-Through Facility
SQL pass-through facility
With the pass-through facility, you can perform a native SQL query inside a DBMS using PROC SQL. To accomplish this, perform the following tasks inside the SQL procedure:
  1. Establish a connection with the DBMS by using a CONNECT statement.
  2. Retrieve data from the DBMS to be used in a PROC SQL query with the CONNECTION TO component in a SELECT statement's FROM clause.
  3. Terminate the connection with the DISCONNECT statement.

CONNECT Statement Syntax

The CONNECT statement establishes a connection to send DBMS-specific SQL statements to the DBMS or to retrieve DBMS data. The connection remains in effect until you can issue a DISCONNECT statement or terminate the SQL procedure.
Syntax, CONNECT statement:
PROC SQL;
CONNECT TO DBMS-name <AS alias> (DBMS-connection-options);
QUIT;
dbms-name
identifies the database management system to which you want to connect. You must specify the DBMS name for your SAS/ACCESS interface. You can also specify an optional alias.
alias
specifies for the connection an optional alias that has 1 to 32 characters. If you specify an alias, the keyword AS must appear before the alias. If an alias is not specified, the DBMS name is used as the name of the SQL pass-through connection.
DBMS-connection-options
specifies values for DBMS-specific arguments that PROC SQL needs in order to connect to the DBMS. Though they are optional for most databases, you must enclose the values in parentheses if you include any.
The CONNECT statement establishes a connection with the DBMS. Depending on your DBMS setup, you might have to specify specific DBMS connection arguments.
Once you connect to the DBMS, you can submit SELECT or EXECUTE statements.

DISCONNECT Statement Syntax

The DISCONNECT statement ends the connection with the DBMS. If you do not include the DISCONNECT statement, SAS performs an implicit DISCONNECT when PROC SQL terminates. The SQL procedure continues to execute until you submit a QUIT statement, another SAS procedure, or a DATA step. Any return code or message that is generated by the DBMS is available in the macro variables SQLXRC and SQLXMSG after the statement executes.
Syntax, DISCONNECT statement:
PROC SQL;
CONNECT TO dbms-name <AS alias> (DBMS-connection-options);
DISCONNECT FROM dbms-name | alias
QUIT;
dbms-name
specifies the database management system from which you want to disconnect. You must either specify the DBMS name for your SAS/ACCESS interface or use an alias in the DISCONNECT statement.
Note: If you used the CONNECT statement to connect to the DBMS, the DBMS name or alias in the DISCONNECT statement must match what you specified in the CONNECT statement.
alias
specifies an alias that was defined in the CONNECT statement.

Example: Connecting to an Oracle Database

This example connects to Oracle and performs a simple query on the Customers table.
proc sql;
   connect to oracle (user=User password=Student827 path=localhost);
   select *
      from connection to oracle
      (select * from customers
       where customer like '1%');
   disconnect from oracle;
quit;

Additional Notes about the SQL Pass-Through Facility

When you are using the SQL pass-through facility, there are a few advantages and disadvantages to keep in mind. The DBMS optimizes data summarization, ordering tables, joining, and querying because DBMS SQL code is executed within the DBMS. Any DBMS-specific functions and features can be used. You can also combine SAS features with DBMS-specific features within your query. For example, you can use SAS labels and formats with DBMS-specific features. The results of your query can be saved as a SAS data file or SAS view.
Only the SQL code that is within the parentheses is passed to the DBMS. If you are returning results that contain an ORDER BY clause outside the DBMS-specific SQL query, the ordering of the table occurs in SAS. This also applies to labels and formats. If you specify WHERE processing or sorting within SAS step (for example, as an SQL view), the SQL view executes the instructions within the DBMS. However, the results that are returned are executed by SAS.

SAS/ACCESS LIBNAME Engine

A Brief Overview

If you are familiar with the LIBNAME statement, recall that it enables you to assign a library reference, or an alias, to a SAS library. The syntax for the LIBNAME statement is the keyword LIBNAME, followed by the library reference name, the location of your SAS data in quotation marks, and some options. When you name a library reference, the name can be between one and eight characters long. The name must begin with a letter or an underscore, and it can continue with any number of characters, letters, or numbers and underscores up to eight.
For example, suppose your company uses Teradata, and you need to connect to the database.
When you are using the SAS/ACCESS LIBNAME statement, it enables you to make a connection to Teradata. It establishes a library reference that acts as an alias, or a nickname, to Teradata. This enables you to use the Teradata tables with the SAS syntax, where you reference the table name as a two-level name. It also enables you to use the Teradata table in order to update the table, if you have the correct authority to do so. The SAS/ACCESS LIBNAME statement also enables you to use SAS/ACCESS LIBNAME options to specify how Teradata objects are processed by SAS. Last, it enables you to customize how to connect to Teradata.
When you are using PROC SQL with the SAS/ACCESS LIBNAME engine connection to a database, the engine converts PROC SQL syntax to the native SQL of the database wherever possible.
Figure 6.3 SAS/ACCESS LIBNAME Statement
SAS/ACCESS LIBNAME Statement
When you are reading data from a specific database, each SAS/ACCESS LIBNAME statement provides an engine name as part of the LIBNAME statement syntax.

The SAS/ACCESS LIBNAME Statement

The LIBNAME statement enables you to assign a library reference, or an alias, to a database. To use the SAS/ACCESS LIBNAME statement, specify a libref, engine-name, and necessary connection options to connect to the database.
Syntax, SAS/ACCESS LIBNAME statement:
LIBNAME libref engine <SAS/ACCESS-engine-options>;
  • libref is the name of the SAS library.
  • engine is the name of the SAS/ACCESS engine.
  • SAS/ACCESS-engine-options depend on the SAS/ACCESS engine that you are using.

Closing the DBMS Connection

You can submit a LIBNAME statement with the CLEAR option to release the DBMS and associated resources. It is a good practice to close the DBMS connection when you end your session.
LIBNAME libref clear;
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.227.52