Chapter Contents |
Previous |
Next |
CONNECT Statement |
Syntax | |
Arguments | |
ODBC Connection Arguments | |
CONNECT Examples | |
Tips for Connecting to a Microsoft Excel Data Source |
Syntax |
CONNECT TO ODBC <AS alias > <(ODBC-connection-arguments)>; |
You use the following arguments with the CONNECT statement:
ODBC Connection Arguments |
PROC SQL supports multiple connections to ODBC. If you use multiple simultaneous
connections, you must use the alias argument to identify the
different connections. If you do not specify an alias, the default alias,
odbc
, is used. The functionality of multiple connections to the same
ODBC data source may be limited by the particular data source's driver.
The CONNECT statement is required when connecting to ODBC data sources by way of the SQL Pass-Through Facility.
The following list describes the arguments that are used for ODBC in the CONNECT statement. You can use the arguments DSN=, UID=, and PWD= to connect to most data sources. Use the PROMPT, NOPROMPT, COMPLETE, REQUIRED, or AUTOCOMMIT arguments to provide additional information or to select and connect to the data source.
The arguments for the DBMS connection information arguments can be quoted by using either single or double quotes. Some values may include embedded spaces, semicolons, or quotes and, therefore, must be quoted.
Note: Not all of these engine connection options are supported
by all ODBC drivers. Refer to your vendor-supplied documentation for more
information.
There are multiple ways that you can connect to the DBMS when using the CONNECT statement. Use only one of the following methods for each connection since they are mutually exclusive:
If AUTOCOMMIT=YES, no rollback is possible.
If AUTOCOMMIT=NO, the SAS/ACCESS engine automatically does the commit when it reaches the end of the file.
The default value for AUTOCOMMIT= under ODBC is NO if the ODBC driver supports transactions and the connection is used for updating. Otherwise, the default value is YES. The default value is always YES when the PROC SQL Pass-Through facility is used.
.odbc.ini
file. DSN= indicates that the connection is attempted using
the ODBC SQLConnect API, which requires a data source name. Optionally, a
user ID and password (described below) can be used in conjunction with DSN=.
This API is guaranteed to be present in all drivers.
Specify either DSN= or one (and only one) of the following arguments: PROMPT, NOPROMPT, COMPLETE, or REQUIRED. These arguments are all mutually exclusive of each other.
COMPLETE= is similar to the PROMPT= option. However, if COMPLETE= attempts to connect and fails, then a dialog box is displayed and you can edit values or enter additional values.
COMPLETE= is optional.
See your driver documentation for more details.
A dialog box is displayed, using the values from the PROMPT= connection string. You can edit any field before you connect to the data source. When a successful connection is made, the complete connect string is returned in the SYSDBMSG macro variable.
PROMPT= is similar to the COMPLETE= option. However, unlike COMPLETE=, PROMPT= does not attempt to connect to the DBMS first. It displays the dialog box where you can edit or enter additional values.
If you specify enough correct connection options, such as user ID, password, and data source name, the SAS/ACCESS engine connects to the data source or database. Otherwise, a dialog box is displayed to prompt you for the connection options. Options in the dialog box that are not related to the connection are disabled. REQUIRED= only allows you to modify required fields in the dialog box. When a successful connection is made, the complete connect string is returned in the SYSDBMSG macro variable.
REQUIRED= is similar to COMPLETE= because it attempts to connect to the DBMS first. However, if REQUIRED= attempts to connect and fails, then a dialog box is displayed and you can only edit values that are in the required fields.
REQUIRED= is optional.
CONNECT Examples |
These examples
use ODBC to connect to a data source that is configured under the data source
name
User's Data
using
the alias USER1. The first example uses the connect method that is guaranteed
to be present at the lowest level of ODBC conformance. Note that DSN= names
may contain quotes and spaces.
proc sql; connect to ODBC as user1 (dsn="User's Data" uid=testuser pwd=testpass);
The next example uses the connect method that represents a more advanced level of ODBC conformance. It uses the input dialog box that is provided by the driver. The DSN= and UID= arguments are within the connect string and, therefore, are not parsed by the Pass-Through facility but instead are passed to the ODBC manager.
proc sql; connect to odbc as user1 (required = "dsn=User's Data;uid=testuser");
The next ODBC example enables you to select any data source that is configured on your machine. The example uses the connect method that represents a more advanced level of ODBC conformance, Level 1. When a successful connection is made, the connect string is returned in the SQLXMSG and SYSDBMSG macro variables and can be stored if this method is used to configure a connection for later use.
proc sql; connect to odbc (required);
This last ODBC example prompts you to specify the information that is required to make a connection to the DBMS. You are prompted to supply the data source name, user ID, and password in the dialog boxes that are displayed.
proc sql; connect to odbc (prompt);
Tips for Connecting to a Microsoft Excel Data Source |
Multiple named ranges can exist within an Excel file. Each one would be treated as a separate table.
You must also create a data source name within the ODBC Administrator on your PC. Within the ODBC Administrator, select the Microsoft Excel ODBC driver and create a DSN. For this example, the DSN is named SALES_97 and is based on the Excel file NEWSALES.XLS. The following example will create a view and print the contents of the named range, SALES_Q1, from the Excel file NEWSALES.XLS.
proc sql; connect to odbc as mydb (dsn=sales_97); create view regsales as select * from connection to mydb (select * from sales_q1); quit; proc print data=regsales; run;
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.