Download Getting Oracle Data into SAS® Datasets

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Concurrency control wikipedia , lookup

Business intelligence wikipedia , lookup

Data vault modeling wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Versant Object Database wikipedia , lookup

Database wikipedia , lookup

SQL wikipedia , lookup

Microsoft Access wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

PL/SQL wikipedia , lookup

Oracle Database wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Transcript
NESUG 15
Pharmaceuticals
Getting Oracle Data into SAS® Datasets
Alexa Parliyan, Pfizer Inc., New York, NY
ABSTRACT
ODBC ADMINISTRATOR
The ODBC Administrator needs to be set up only once for users
with the SAS/ACCESS Interface to ODBC license . As mentioned,
the SAS/ACCESS Interface to Oracle does not need this step.
For the Windows environment, find the ODBC or ODBC
Administrator icon. This icon may be located in the Control Panel,
in an ODBC group, or some other related group. Next, Add a new
User Data Source, and enter the exact Data Source Name,
Description, User Name and Server as provided by the Oracle
expert. That’s it! It’s very simple, once you have the database
information. The screen captures below, figure 1, are an example
of the ODBC Administrator found in the Control Panel, and
configured for a database called Tracking. This process is fully
described in the SAS Notes document TS-626 SAS ODBC Driver
Technical Report: User’s Guide and Programmer’s Reference.
ODBC? SAS/ACCESS? Confused? To access an Oracle
database from a SAS program requires only a few, simple set-up
tasks. This paper will give you step-by-step instructions on how to
create SAS programs that report on Oracle data, using the
author’s own experiences in a Windows PC environment.
INTRODUCTION
Retrieving data from an Oracle database within a SAS program is
simple. The hard part is setting up your system so that you can
access the Oracle database. The good news is that it is not really
so difficult! This paper explores the set up needed for a Windows
PC environment, and simple retrieval techniques that may be
implemented within any SAS program.
FIGURE 1 – CONFIGURING THE ODBC ADMINISTRATOR
SET UP
To begin, you will need to know about your SAS licenses, and you
will need to find an Oracle subject matter expert who knows the
location of your Oracle database.
SAS LICENSES
There are two SAS licenses that will allow you to access Oracle
data in the Windows environment:
1. SAS/ACCESS Interface to ODBC
2. SAS/ACCESS Interface to Oracle
Both licenses require that you have an Oracle Client installed and
a communication protocol called SQL*NET in Oracle 6 and 7 or
NET80 in Oracle 8 and up. The Oracle Client configuration, which
mainly involves editing files such as sqlnet.ora and tnsnames.ora,
should be done by an Oracle expert. Care must be taken with
version conflicts. As always, PC configuration can be tricky.
There are three key differences between the two licenses:
1. The ODBC interface is free with BASE SAS for Windows.
The Oracle interface must be licensed separately.
2. The ODBC interface will allow you to access databases
other than Oracle, such as FoxPro, dBase and MS-Access.
3. For the ODBC interface, every machine must have an ODBC
Administrator (different than the above mentioned Oracle
Client) configured with the description of the specific
database that you need to access. The Oracle interface
does not require this. Originally, we used the ODBC
interface, and configured each machine with the ODBC
Administrator. However, since we have many users working
remotely via Citrix, this did not work in all situations. The
Oracle interface works well for us when we may not be using
SAS locally.
ORACLE DATABASE INFORMATION
For either interface you will need to know the data source, user
ID, and password to access your database. You will need to get
this information from the Oracle expert. This information is used in
the ODBC Administrator on your local machine (for the ODBC
interface), and also in your SAS program statements to access
the database (for either interface).
1
NESUG 15
Pharmaceuticals
ACCESSING THE DATA
The SAS/ACCESS Interface to ODBC and Oracle products
provides support for the SQL Procedure Pass-Through Facility,
the ACCESS procedure and the DBLOAD Procedure. With SAS
Release 8, the LIBNAME engine support was added to the
SAS/ACCESS product.
SQL PASSTHRU FACILITY
Using PROC SQL is one way to access the data within a SAS
program that works in both Version 6.12 and Version 8. No
LIBNAME statements are needed – the connection may be made
right within the SQL procedure. The syntax is simply to start the
SQL procedure, connect to the database by passing the database
name, user name, and password, then selecting the variables of
interest. To close, simply disconnect from the database.
There are only minor differences when using the different
interfaces. Below are two examples of accessing an Oracle
database called Tracking using the SQL Pass Through technique,
with differences in bold.
SQL EXAMPLE
USING SAS/ACCESS Interface to Oracle:
SQL EXAMPLE
USING SAS/ACCESS Interface to ODBC:
proc sql;
proc sql;
connect to odbc
(dsn=tracking uid=parliyan pwd=pass1);
connect to oracle as oracle
(user=parliyan orapw=pass1 path=tracking);
create table studies as
select * from connection to odbc
(select a.protocol,
b.priority, b.dbflag
from master a, sflag b
where a.protocol = b.protocol);
create table studies as
select * from connection to oracle
(select a.protocol,
b.priority, b.dbflag
from master a, sflag b
where a.protocol = b.protocol);
create table function as
select * from connection to odbc
(select a.protocol,
b.funcdate, b.funccode,
b.functext
from master a, sfunc b
where a.funccode = b.funccode);
create table function as
select * from connection to oracle
(select a.protocol,
b.funcdate, b.funccode,
b.functext
from master a, sfunc b
where a.funccode = b.funccode);
disconnect from odbc;
quit;
disconnect from oracle;
quit;
proc sort data=studies;
by protocol;
run;
proc sort data=function;
by protocol;
run;
proc sort data=studies;
by protocol;
run;
proc sort data=function;
by protocol;
run;
2
NESUG 15
Pharmaceuticals
USING LIBNAMES
Beginning with Version 8, in addition to using SQL to access the
database tables, you can use a libname and access them like
SAS datasets. This works so well, since you can then use simple
SAS code for accessing any table, exactly as if it is a SAS
dataset. The syntax is the same as a normal libname statement,
with the engine used (odbc or oracle), and some options:
SAS VERSION VARIABILITY
In addition to an inability to use the LIBNAME statement in
Version 6.12, the other difference we found between Version 6.12
and Version 8.02 with regard to accessing Oracle data was the
variable name length issue. Since Oracle variable names can be
long, but Version 6.12 does not support long names, we had
written all of our programs with the assumption that the variable
names would be truncated. When we converted to Version 8.02,
we added the following option so that the SAS variable names
would still be short: options VALIDVARNAME=V6;
Thus we did not have to re-write our programs. Another issue to
consider is that SAS Version 6.12 variables have a length
maximum of 200. When pulling data out of Oracle, the length is
shortened, and data may be truncated. We found that several
columns (variables) we worked with in Oracle were arbitrarily set
to 300 characters – when we pulled them into SAS Version 6.12,
the length was truncated to 200, while when we pulled the tables
into Version 8 the variables were not truncated.
LIBNAME [libref] [engine] [engine options]
preserve_tab_names=yes preserve_col_names=yes;
The engine would be either ODBC or Oracle.
To avoid confusion, one of the options that should be used is
schema=[public/admin/dbo/etc.]. You will need to query
your Oracle database to find the schema (sometimes referred to
as owner) of the tables you want to access. If the schema is not
explicitly defined, it will default to the username given for the
database. To view all tables in a database, you will need to create
separate libnames for each schema if there are different
schemas.
COMMON PROBLEMS
Some common errors that may occur if you do not have
everything set up correctly:
•
ERROR: ORACLE connection error: ORA-12154:
TNS:could not resolve service name.
•
ERROR: Connection to the oracle DBMS does
not exist.
•
ERROR: ORACLE connection error: ORA-01017:
invalid username/password; logon denied.
Problems that we have encountered include:
SAS license for Oracle not current
the server with the Oracle database being down
ODBC Administrator not set up
Oracle Client not installed
Database name, user ID or password incorrect
The options, preserve_tab_names and preserve_col_names,
should also be considered, since this may affect your access as
well. Preserve_tab_names=no and preserve_col_names=no are
the default. These options control the table name (dataset name)
and column name (variable name) case sensitivity, that is,
whether the value in the Oracle database is upper-case, lowercase or mixed-case.
LIBNAME EXAMPLE
USING SAS/ACCESS Interface to ODBC:
libname safetr odbc
datasrc='tracking'
user='parliyan'
password='pass1'
schema=admin
preserve_tab_names=yes
preserve_col_names=yes;
CONCLUSION
This paper hopes to show how to set up your Windows PC
environment so that you can access Oracle data from within your
SAS programs. Since we have done this, we have been able to
write numerous reports from data that were previously dumped
first into an Excel file, and then into SAS, which was a time
consuming and inefficient process.
LIBNAME EXAMPLE
USING SAS/ACCESS Interface to Oracle:
libname safetr oracle
datasrc='tracking'
user='parliyan'
password='pass1'
schema=admin
preserve_tab_names=yes
preserve_col_names=yes;
REFERENCES
•
•
•
•
Once the libname is set up you can address the tables as if they
were SAS datasets. The only thing you can't do is write new rows
to the table - this includes sorting. If you have the access
permissions, you can, however, update and delete values within
rows.
TS-626: SAS ODBC Driver Technical Report: User’s Guide
and Programmer’s Reference
TS-566D: Processing ORACLE® Dates with SAS/ACCESS®
in the UNIX® Environment
SN-002479: Unable to read tables or columns that contain
lower-case, mixed-case or special characters in their names
after assigning a SAS/ACCESS library
Some excellent examples are on the SAS website:
http://ftp.sas.com/techsup/download/sample/samp_lib/
orlsampSASACCESS_for_ORACLE_Sample_Prog.html
ACKNOWLEDGMENTS
Thanks to Fan Zhou, Jim Crowe, Dan Gronell, Virginia Ott, Chris
Roper and Ken Salatka for their review and input.
PROC DBLOAD and PROC ACCESS
Data can be loaded to the Oracle database using PROC
DBLOAD and PROC ACCESS. Excellent examples may be
found on the SAS website in the Technical Support, Sample
programs. We have not used this, so we will not go into further
detail here.
CONTACT INFORMATION
For comments and questions, please contact the author:
Alexa Parliyan
Pfizer, Inc.
235 East 42nd Street
New York, NY 10017
212-733-6584
[email protected]
3