You are on page 1of 4

So your data is on multiple servers?

No worries SAS can help


David Lawrence, Comcast, Manchester, NH
ABSTRACT
In my organization there is a massive amount of data that is stored across multiple servers. This is further
complicated by the fact the servers are often on different platforms. Some of our primary servers are Oracle,
SQL Server, Teradata and even home grown databases using Microsoft Access and Microsoft Excel flat
files. Im sure my situation is not unique. SAS has proven to be a great tool for combining data from multiple
servers without the necessity of creating unneeded data sets or exports to flat files.

INTRODUCTION
In this paper I will show you how to create an ODBC connection to your database and demonstrate both an
implicit and explicit pass-through query. The method I use to connect to two servers simultaneously and how to
use a Data Set as reference table when connecting to a remote server will also be shown.
Since I will be connecting to relational databases I will primarily use PROC SQL, however, the DATA step can
also be used for many of these scenarios. It is helpful if the reader has a general understanding of SQL
programming and how to interact with relational databases. This paper will focus on connecting in a Windows
environment, but these techniques should also translate to a UNIX system. It is assumed the reader has BASE
SAS installed and is licensed for SAS/ACCESS. If you are unsure of your licensing, you can run the PROC
SETINIT code below.
PROC SETINIT; RUN;

GETTING STARTED ODBC SETUP


To create an ODBC connection to your database select Control Panel from the Start Menu and then
Administrative Tools. This will bring you to the window below where you can choose the Data Sources (ODBC)
menu. Your options may be slightly different depending on if you have a 32 bit or 64 bit machine and your
computer setup.

When the Data Source Administrator is selected you will have the ability to create a user or system DSN. The
system DSN will be universal to all users of the computer, and the user DSN is specific to you. I show below how
to connect to a SQL Server environment.
For a new connection select Add and you will be prompted for the server type, and then the wizard will prompt
you for the specific steps related to that server. The SQL Server configuration will allow you to select a server
and offer an option to connect using NT login credentials or a specific user name and password. The name you
select will be used as the reference for your LIBNAME statement.

Although it is not absolutely necessary to create a connection via ODBC, I find it easier and more efficient if it is a
server I use regularly and I can only connect via ODBC. For more information on ODBC connections please see
Laura Liotus paper on this topic http://www.nesug.org/Proceedings/nesug10/cc/cc20.pdf

ACCESSING THE DATA


As I mentioned in the introduction, I will primarily be using a LIBNAME (implicit pass through) statement instead of
explicit pass through because the explicit statement will not allow a direct join between multiple servers. However,
you can create a dataset using the explicit method, and use that as a reference table to connect to an implicit
connection of a database. When you access the server via a LIBNAME, SAS optimizes the SQL code and
passes as much of the code as possible to the database server. There have been improvements made starting in
SAS 9.2. However, the bulk of the work is done locally and complex joins and server specific syntax may not work

as expected. By using explicit pass-through, the code is passed directly to the server and is also processed
there.
For a more thorough explanation of this topic please see Jessica Hamptons paper
http://www.nesug.org/Proceedings/nesug11/ps/ps04.pdf.
I will first demonstrate connecting to two servers simultaneously -- one is an Oracle server and the other SQL
Server. Youll notice when assigning the LIBNAME I am connecting via ODBC and ORA. This is based on how
my SAS/Access is licensed and may vary in your environment. The DSN in the first statement refers to my
configuration name in the ODBC setup. The same LIBNAME will be used for all samples.
In this sample I am connecting to the Oracle and SQL Server databases and returning a small amount of data.
The SQL statement returned 107 rows in less than one minute. I feel this is a very efficient method that requires
no intermediate data sets or steps to return the data.
libname OT odbc user=me password=pwd dsn=OT stringdates=yes schema=dbo; run;
libname ora oracle user=user password=pwd path=server' schema=schema; run;
proc sql;
create table join_tables as
select ot.fixcodeid, ot.desc, o.category
from ora.fix_del as o
inner join ot.fixcode as ot
on o.fixcodeid = ot.fixcodeid;
quit;
I really like the ability to connect to multiple servers at the same time, however, this is not always possible due to
performance of the databases, or there is simply too much data to retrieve. When this happens I will create one
or more smaller data sets to join to the database tables.
In this sample I am creating a data set of the SQL Server table and then joining the data set to the Oracle table. I
used a data step to create the reference data set, but PROC SQL would work as well.
data fix;
set ontrac.ontrac_dim_fixcode;
run;
proc sql;
create table fix_category as
select ot.fixcodeid, ot.desc, o.category
from ora.fix_del o
inner join work.fix ot
on o.fixcodeid = ot.fixcodeid;
quit;
Below is a sample of an explicit pass through query to return the data from Oracle.

proc sql;
connect to oracle as ora(user=user orapw=pwd path="server.world");
create table fix_del as
select * from connection to ora
(select * from schema.fix_del);
disconnect from ora;
quit;

CONCLUSION
This paper provided a brief overview of how SAS can help you pull data from two different servers simultaneously.
The ability of SAS to connect data across multiple servers and use data sets as a reference is one of my favorite
features of SAS as it has helped me streamline many of my processes.

REFERENCES
Hampton, Jessica. 2011. SQL Pass-Through and the ODBC Interface.
http://www.nesug.org/Proceedings/nesug11/ps/ps04.pdf
liotus L.. 2010 Get Connected - Attach To Your Data Sources Using SAS/ACCESS
http://www.nesug.org/Proceedings/nesug10/cc/cc20.pdf
Capobianco, F. 2011 Explicit SQL Pass-Through: Is It Still Useful?
http://support.sas.com/resources/papers/proceedings11/105-2011.pdf

TRADEMARK INFORMATION
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 registered trademarks or trademarks of their respective companies.

CONTACT INFORMATION
Your comments and questions are valued and encouraged. Contact the author at:
David Lawrence
Senior Analyst at Comcast
676 Island Pond Rd.
Manchester, NH 03109
Email: djlawre@outlook.com

You might also like