如何读取 SAS 数据集? [英] How can I read a SAS dataset?

查看:36
本文介绍了如何读取 SAS 数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多 SAS 格式的文件,我希望能够在 SAS 之外的程序中读取它们.除了安装的基本 SAS 系统之外,我什么都没有.我可以手动转换每一个,但我想要一种自动转换的方法.

I have a lot of files in SAS format, and I'd like to be able to read them in programs outside of SAS. I don't have anything except the base SAS system installed. I could manually convert each one, but I'd like a way to do it automatically.

推荐答案

您需要有一个正在运行的 SAS 会话来充当数据服务器.然后您可以使用 ODBC 访问 SAS 数据,请参阅 SAS ODBC 驱动程序指南.

You'll need to have a running SAS session to act as a data server. You can then access the SAS data using ODBC, see the SAS ODBC drivers guide.

要运行本地 SAS ODBC 服务器,您需要:

To get the local SAS ODBC server running, you need to:

  1. 在 SAS ODBC 驱动程序指南中定义您的 SAS ODBC 服务器设置.在下面的示例中,我将连接到一个名为loclodbc"的服务器.
  2. 在您的服务文件(C:\WINDOWS\system32\drivers\etc\services)中添加一个条目,如下所示:

  1. Define your SAS ODBC server setup at described in the SAS ODBC drivers guide. In the example that follows, I'll connect to a server that is set up with the name "loclodbc".
  2. Add an entry in your services file, (C:\WINDOWS\system32\drivers\etc\services), like this:

  • loclodbc 9191/tcp

...设置端口号(此处:9191),使其适合您的本地设置.服务loclodbc"的名称必须与 ODBC 设置中定义的服务器名称相匹配.请注意,术语服务器"与您的 PC 的物理主机名无关.

...set the port number (here: 9191) so that it fits into your local setup. The name of the service "loclodbc" must match the server name as defined in the ODBC setup. Note that the term "Server" has nothing to do with the physical host name of your PC.

您的 SAS ODBC 服务器现在已准备好运行,但没有可用的指定数据资源.通常,您会在 SAS ODBC 设置过程的库"选项卡中进行设置,但由于您想即时"指向数据源,因此我们省略了此设置.

Your SAS ODBC server is now ready to run, but is has no assigned data resources available. Normally you would set this in the "Libraries" tab in the SAS ODBC setup process, but since you want to point to data sources "on the fly", we omit this.

您现在可以从您的客户端应用程序连接到 SAS ODBC 服务器,指向您想要访问的数据资源并获取数据.

From your client application you can now connect to the SAS ODBC server, point to the data resources you want to access, and fetch the data.

SAS 指向数据资源的方式是通过LIBNAME"的概念.libname 是指向数据集合的逻辑指针.

The way SAS points to data resources is through the concept of the "LIBNAME". A libname is a logical pointer to a collection of data.

因此

LIBNAME sasadhoc 'C:\sasdatafolder';

为文件夹C:\sasdatafolder"分配逻辑句柄sasiodat".

assigns the folder "C:\sasdatafolder" the logical handle "sasiodat".

如果您在 SAS 内部想要访问驻留在 SAS 数据表文件C:\sasdatafolder\test.sas7bdat"中的数据,您可以执行以下操作:

If you from within SAS want access to the data residing in the SAS data table file "C:\sasdatafolder\test.sas7bdat", you would do something like this:

LIBNAME sasadhoc 'C:\sasdatafolder';
PROC SQL;
  CREATE TABLE WORK.test as
  SELECT *
  FROM sasadhoc.test
  ;
QUIT;

所以我们需要做的是告诉我们的 SAS ODBC 服务器从我们的客户端应用程序中将 libname 分配给 C:\sasdatafolder.我们可以通过使用 DBCONINIT 参数在启动时向其发送资源分配请求来实现这一点.

So what we need to do is to tell our SAS ODBC server to assign a libname to C:\sasdatafolder, from our client application. We can do this by sending it this resource allocation request on start up, by using the DBCONINIT parameter.

我为此编写了一些示例代码.我的示例代码也是用 BASE SAS 语言编写的.由于访问SAS数据的方式显然比SAS通过ODBC连接SAS更聪明,所以此代码仅作为示例.

I've made some sample code for doing this. My sample code is also written in the BASE SAS language. Since there are obviously more clever ways to access SAS data, than SAS connecting to SAS via ODBC, this code only serves as an example.

您应该能够利用有用的部分并在您使用的编程环境中创建自己的解决方案...

You should be able to take the useful bits and create your own solution in the programming environment you're using...

SAS ODBC 连接示例代码:

SAS ODBC connection sample code:

PROC SQL;
  CONNECT TO ODBC(DSN=loclodbc DBCONINIT="libname sasadhoc 'c:\sasdatafolder'");
  CREATE TABLE temp_sas AS
  SELECT * FROM CONNECTION TO ODBC(SELECT * FROM sasadhoc.test);
QUIT;

神奇发生在代码的CONNECT TO ODBC..."部分,为所需数据所在的文件夹分配一个libname.

The magic happens in the "CONNECT TO ODBC..." part of the code, assigning a libname to the folder where the needed data resides.

这篇关于如何读取 SAS 数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆