Programming with the SQL Procedure |
SAS/ACCESS software for relational databases provides an interface between SAS software and data in other vendors' database management systems (DBMSs). SAS/ACCESS software provides dynamic access to DBMS data through the SAS/ACCESS LIBNAME statement and the PROC SQL Pass-Through Facility. The LIBNAME statement enables you to assign SAS librefs to DBMS objects such as schemas and databases. The Pass-Through Facility enables you to interact with a DBMS by using its SQL syntax without leaving your SAS session.
It is recommended that you use the SAS/ACCESS LIBNAME statement to access your DBMS data because it is usually the fastest and most direct method of accessing DBMS data. The LIBNAME statement offers the following advantages:
Significantly fewer lines of SAS code are required to perform operations in your DBMS. For example, a single LIBNAME statement establishes a connection to your DBMS, enables you to specify how your data is processed, and enables you to easily browse your DBMS tables in SAS.
You do not need to know your DBMS's SQL language to access and manipulate your DBMS data. You can use SAS procedures, such as PROC SQL, or DATA step programming on any libref that references DBMS data. You can read, insert, update, delete, and append data, as well as create and drop DBMS tables by using normal SAS syntax.
The LIBNAME statement provides more control over DBMS operations such as locking, spooling, and data type conversion through the many LIBNAME options and data set options.
The LIBNAME engine optimizes the processing of joins and WHERE clauses by passing these operations directly to the DBMS to take advantage of the indexing and other processing capabilities of your DBMS.
An exception to this recommendation occurs when you need to use SQL that does not conform to the ANSI standard. The SAS/ACCESS LIBNAME statement accepts only ANSI-standard SQL, but the PROC SQL Pass-Through Facility accepts all the extensions to SQL that are provided by your DBMS. Another advantage of this access method is that Pass-Through Facility statements enable the DBMS to optimize queries when the queries have summary functions (such as AVG and COUNT), GROUP BY clauses, or columns that were created by expressions (such as the COMPUTED function).
For more information about SAS/ACCESS software, see SAS/ACCESS for Relational Databases: Reference.
Connecting to a DBMS by Using the LIBNAME Statement |
Use the LIBNAME statement to read from and write to a DBMS object as if it were a SAS data set. After connecting to a DBMS table or view by using the LIBNAME statement, you can use PROC SQL to interact with the DBMS data.
For many DBMSs, you can directly access DBMS data by assigning a libref to the DBMS by using the SAS/ACCESS LIBNAME statement. Once you have associated a libref with the DBMS, you can specify a DBMS table in a two-level SAS name and work with the table like any SAS data set. You can also embed the LIBNAME statement in a PROC SQL view (see the "CREATE VIEW Statement in the Base SAS Procedures Guide).
PROC SQL takes advantage of the capabilities of a DBMS by passing it certain operations whenever possible. For example, before implementing a join, PROC SQL checks to determine whether the DBMS can perform the join. If it can, then PROC SQL passes the join to the DBMS, which enhances performance by reducing data movement and translation. If the DBMS cannot perform the join, then PROC SQL processes the join. Using the SAS/ACCESS LIBNAME statement can often provide you with the performance benefits of the SQL Procedure Pass-Through Facility without writing DBMS-specific code.
Note: You can use the DBIDIRECTEXEC system option to send a PROC SQL CREATE TABLE AS SELECT statement or a DELETE statement directly to the database for execution, which could result in CPU and I/O performance improvement. For more information, see the SAS/ACCESS documentation for your DBMS.
To use the SAS/ACCESS LIBNAME statement, you must have SAS/ACCESS software installed for your DBMS. For more information about the SAS/ACCESS LIBNAME statement, see the SAS/ACCESS documentation for your DBMS.
This example uses PROC SQL to query the ORACLE table PAYROLL. The PROC SQL query retrieves all job codes and provides a total salary amount for each job code.
libname mydblib oracle user=user-id password=password path=path-name schema=schema-name; proc sql; select jobcode label='Jobcode', sum(salary) as total label='Total for Group' format=dollar11.2 from mydblib.payroll group by jobcode; quit;
Output from Querying a DBMS Table
Total for Jobcode Group BCK $232,148.00 FA1 $253,433.00 FA2 $447,790.00 FA3 $230,537.00 ME1 $228,002.00 ME2 $498,076.00 ME3 $296,875.00 NA1 $210,161.00 NA2 $157,149.00 PT1 $543,264.00 PT2 $879,252.00 PT3 $21,009.00 SCP $128,162.00 TA1 $249,492.00 TA2 $671,499.00 TA3 $476,155.00
PROC SQL views are stored query expressions that read data values from their underlying files, which can include SAS/ACCESS views of DBMS data. While DATA step views of DBMS data can be used only to read the data, PROC SQL views of DBMS data can be used to update the underlying data if the following conditions are met:
The PROC SQL view is based on only one DBMS table (or on a DBMS view that is based on only one DBMS table).
The PROC SQL view has no calculated fields.
The following example uses the LIBNAME statement to connect to an ORACLE database, create a temporary PROC SQL view of the ORACLE table SCHEDULE, and print the view by using the PRINT procedure. The LIBNAME engine optimizes the processing of joins and WHERE clauses by passing these operations directly to the DBMS to take advantage of DBMS indexing and processing capabilities.
libname mydblib oracle user=user-id password=password proc sql; create view LON as select flight, dates, idnum from mydblib.schedule where dest='LON'; quit; proc print data=work.LON noobs; run;
Output from the PRINT Procedure
FLIGHT DATES IDNUM 219 04MAR1998:00:00:00 1739 219 04MAR1998:00:00:00 1478 219 04MAR1998:00:00:00 1130 219 04MAR1998:00:00:00 1125 219 04MAR1998:00:00:00 1983 219 04MAR1998:00:00:00 1332 219 05MAR1998:00:00:00 1428 219 05MAR1998:00:00:00 1442 219 05MAR1998:00:00:00 1422 219 05MAR1998:00:00:00 1413 219 05MAR1998:00:00:00 1574 219 05MAR1998:00:00:00 1332 219 06MAR1998:00:00:00 1106 219 06MAR1998:00:00:00 1118 219 06MAR1998:00:00:00 1425 219 06MAR1998:00:00:00 1434 219 06MAR1998:00:00:00 1555 219 06MAR1998:00:00:00 1332
Connecting to a DBMS by Using the SQL Procedure Pass-Through Facility |
The SQL Procedure Pass-Through Facility enables you to send DBMS-specific SQL statements directly to a DBMS for execution. The Pass-Through Facility uses a SAS/ACCESS interface engine to connect to the DBMS. Therefore, you must have SAS/ACCESS software installed for your DBMS.
You submit SQL statements that are DBMS-specific. For example, you pass Transact-SQL statements to a Sybase database. The Pass-Through Facility's basic syntax is the same for all the DBMSs. Only the statements that are used to connect to the DBMS and the SQL statements are DBMS-specific.
With the Pass-Through Facility, you can perform the following tasks:
Establish a connection with the DBMS by using a CONNECT statement and terminate the connection with the DISCONNECT statement.
Send nonquery DBMS-specific SQL statements to the DBMS by using the EXECUTE statement.
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.
You can use the Pass-Through Facility statements in a query, or you can store them in a PROC SQL view. When a view is stored, any options that are specified in the corresponding CONNECT statement are also stored. Thus, when the PROC SQL view is used in a SAS program, SAS can automatically establish the appropriate connection to the DBMS.
For more information, see the CONNECT statement, the DISCONNECT statement, the EXECUTE statement, and the CONNECTION TO statement in the Base SAS Procedures Guide, and The Pass-Through Facility for Relational Databases in SAS/ACCESS for Relational Databases: Reference.
Note: SAS procedures that perform multipass processing cannot operate on PROC SQL views that store Pass-Through Facility statements, because the Pass-Through Facility does not allow reopening of a table after the first record has been retrieved. To work around this limitation, create a SAS data set from the view and use the SAS data set as the input data set.
As you use PROC SQL statements that are available in the Pass-Through Facility, any errors are written to the SAS log. The return codes and messages that are generated by the Pass-Through Facility are available to you through the SQLXRC and SQLXMSG macro variables. Both macro variables are described in Using the PROC SQL Automatic Macro Variables.
In this example, SAS/ACCESS connects to an ORACLE database by using the alias ora2 , selects all rows in the STAFF table, and displays the first 15 rows of data by using PROC SQL.
proc sql outobs=15; connect to oracle as ora2 (user=user-id password=password); select * from connection to ora2 (select lname, fname, state from staff); disconnect from ora2; quit;
Output from the Pass-Through Facility Example
LNAME FNAME STATE --------------------------------------- ADAMS GERALD CT ALIBRANDI MARIA CT ALHERTANI ABDULLAH NY ALVAREZ MERCEDES NY ALVAREZ CARLOS NJ BAREFOOT JOSEPH NJ BAUCOM WALTER NY BANADYGA JUSTIN CT BLALOCK RALPH NY BALLETTI MARIE NY BOWDEN EARL CT BRANCACCIO JOSEPH NY BREUHAUS JEREMY NY BRADY CHRISTINE CT BREWCZAK JAKOB CT
Updating PROC SQL and SAS/ACCESS Views |
You can update PROC SQL and SAS/ACCESS views by using the INSERT, DELETE, and UPDATE statements, under the following conditions:
If the view accesses a DBMS table, then you must have been granted the appropriate authorization by the external database management system (for example, DB2). You must have installed the SAS/ACCESS software for your DBMS. For more information about SAS/ACCESS views, see the SAS/ACCESS interface guide for your DBMS.
You can update only a single table through a view. The table cannot be joined to another table or linked to another table with a set-operator. The view cannot contain a subquery.
You can update a column in a view by using the column's alias, but you cannot update a derived column--that is, a column that is produced by an expression. In the following example, you can update the column SS, but not WeeklySalary:
create view EmployeeSalaries as select Employee, SSNumber as SS, Salary/52 as WeeklySalary from employees;
You cannot update a view that contains an ORDER BY.
Note: Beginning with SAS 9, PROC SQL views, the Pass-Through Facility, and the SAS/ACCESS LIBNAME statement are the preferred ways to access relational DBMS data. SAS/ACCESS views are no longer recommended. You can convert existing SAS/ACCESS views to PROC SQL views by using the CV2VIEW procedure. For more information, see the CV2VIEW Procedure in SAS/ACCESS for Relational Databases: Reference.
Copyright © 2008 by SAS Institute Inc., Cary, NC, USA. All rights reserved.