SAS 连PostgreSQL的几种方法

SAS 9.4之前,需要ODBC的方式连接PostgreSQL(记得提前配置好DSN),这些方法不仅仅实用于PostgreSQL,很多常用的数据库都可以通过ODBC或者直接连接的方式访问。

02

  • 方法一,ODBC里执行PostgreSQL的SQL语句
PROC SQL;
CONNECT TO ODBC(DSN="ml"); 

 /* con-Name : is the ODBC connection name. Connection should be pre established */
CREATE TABLE temp_sas AS
SELECT * FROM CONNECTION TO ODBC( SELECT clustername
, clustername::varchar(20) as clustername1,count(*) cnt FROM abc.sms group by 1,2 );
QUIT;

/* Inside the ODBC( )  you can write complex queries 
instead of single table query shown above*/
/*ODBC( )内可以写任何PostgreSQL支持的SQL语法,比如开窗函数,还可以方便支持schema*/
  • 方法二,建逻辑库
libname psql odbc datasrc = 'ml' schema = abc PRESERVE_TAB_NAMES=yes;

proc contents data=psql.sms;run;
  • 方法三,不使用DSN,而是配置ODBC.ini 配置文件(未测试过,这个比较适合Linux环境)
[[ODBC|Data Sources]] 
databasename = the database description

[[database_name]]
Description         = PostgreSQL
Driver              = /usr/lib/x86_64-linux-gnu/odbc/psqlodbca.so
Trace               = No
TraceFile           = /tmp/psqlodbc.log
Database            = databasename
Servername          = servername
UserName            = username
Password            = password
Port                = 5432
ReadOnly            = Yes
RowVersioning       = No
ShowSystemTables    = No
ShowOidColumn       = No
FakeOidIndex        = No
ConnSettings        =
  • 方法四,SAS 9.4开始可以直接连接PostgreSQL等多种数据库了,使用更简单了,
%let server="192.168.0.99";
%let port=5432;
%let user=postgres;
%let pass=xx;
%let db=ml;
%let schema=abc;
%let autocommit=yes

* Connection string;
libname pgdb postgres server=&server. port=&port.
   user=&user. password=&pass. database=&db. autocommit=&autocommit. schema=&schema.;

proc contents data=pgdb.sms;run;

 

  • SAS导入PostgreSQL的注意事项,

PostgreSQL鼓励使用Text(不指定长度的varchar),但SAS会因为它不指定长度,而使用默认的最大长度(1024),这样导致SAS库变得非常大,而访问非常缓慢,如下图所示,在PostgreSQL里,clustername是text,clustername1则是varchar(20),所以记得重新定义下长度,这种情况下,ODBC里执行PostgreSQL的命令相对最好用。

01

 

P.S.

从SAS9.4开始,SAS/ACCESS 接口增加了更多的数据库支持, Amazon Redshift, Aster, DB2, Greenplum, Hadoop, HAWQ, Informix, Impala, Microsoft SQL Server, MySQL, Netezza, ODBC, OLE DB, Oracle, PostgreSQL, SAP HANA, Sybase, Sybase IQ, Teradata, and Vertica.

ref,

  • http://www.cghr.org/sashelp/SAS-ODBC.htm
  • https://help.nceas.ucsb.edu/connect_sas_to_postgresql
  • http://support.sas.com/kb/13/848.html
  • http://support.sas.com/documentation/cdl/en/acreldb/69039/HTML/default/viewer.htm#titlepage.htm