如何读取 SAS 数据集?

发布于 2024-08-23 20:56:35 字数 97 浏览 4 评论 0原文

我有很多 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泅渡 2024-08-30 20:56:35

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

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

  1. 按照 SAS ODBC 驱动程序指南中的描述定义 SAS ODBC 服务器设置。在下面的示例中,我将连接到使用名称“locodbc”设置的服务器。
  2. 在服务文件 (C:\WINDOWS\system32\drivers\etc\services) 中添加一个条目,如下所示:

    • locodbc 9191/tcp

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

您的 SAS ODBC 服务器现在已准备好运行,但没有分配的可用数据资源。通常,您可以在 SAS ODBC 设置过程中的“库”选项卡中设置此项,但由于您想要“动态”指向数据源,因此我们忽略了此项。

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

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

因此,

LIBNAME sasadhoc 'C:\sasdatafolder';

为文件夹“C:\sasdatafolder”分配逻辑句柄“sasiodat”。

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

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

因此,我们需要做的是告诉 SAS ODBC 服务器进行分配从我们的客户端应用程序到 C:\sasdatafolder 的 libname。我们可以通过在启动时使用 DBCONINIT 参数向其发送资源分配请求来完成此操作。

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

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

SAS ODBC 连接示例代码:

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。

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.

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

  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

    ...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.

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.

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.

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.

Thus

LIBNAME sasadhoc 'C:\sasdatafolder';

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

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;

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.

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 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;

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

放飞的风筝 2024-08-30 20:56:35

现在有一个 python 包可以让您读取 .sas7bdat 文件,或者如果您愿意

https: //pypi.python.org/pypi/sas7bdat

There's now a python package that will allow you to read .sas7bdat files, or convert them to csv if you prefer

https://pypi.python.org/pypi/sas7bdat

偏爱你一生 2024-08-30 20:56:35

您可以制作一个 SAS 到 CSV 的转换程序。

将以下内容保存在 sas_to_csv.sas 中:

proc export data=&sysparm
    outfile=stdout dbms=csv;
run;

然后,假设您要访问 libname.dataset,请按如下方式调用此程序:

sas sas_to_csv -noterminal -sysparm "libname.dataset"

SAS 数据转换为可以通过管道传输的 CSV进入Python。在Python中,以编程方式生成“libname.dataset”参数是很容易的。

You could make a SAS-to-CSV conversion program.

Save the following in sas_to_csv.sas:

proc export data=&sysparm
    outfile=stdout dbms=csv;
run;

Then, assuming you want to access libname.dataset, call this program as follows:

sas sas_to_csv -noterminal -sysparm "libname.dataset"

The SAS data is converted to CSV that can be piped into Python. In Python, it would be easy enough to generate the "libname.dataset" parameters programmatically.

从来不烧饼 2024-08-30 20:56:35

我从未尝试过http://www.oview.co.uk/dsread/,但这可能正是您正在寻找的:“一个简单的命令行实用程序,用于处理 SAS7BDAT 文件格式的数据集。”但请注意“该软件应被视为实验性的,不能保证准确。您使用它的风险由您自己承担。它目前仅适用于未压缩的 Windows 格式 SAS7BDAT 文件。”

I have never tried http://www.oview.co.uk/dsread/, but it might be what you're looking for: "a simple command-line utility for working with datasets in the SAS7BDAT file format." But do note "This software should be considered experimental and is not guaranteed to be accurate. You use it at your own risk. It will only work on uncompressed Windows-format SAS7BDAT files for now. "

掌心的温暖 2024-08-30 20:56:35

我想你也许可以使用ADO,
请参阅 SAS 支持网站更多细节。

免责声明:

  1. 我已经有一段时间没有看过这个了,
  2. 我不能 100% 确定这不需要额外的许可,
  3. 我不确定你是否可以使用 Python 来做到这一点

I think you might be able to use ADO,
See the SAS support site for more details.

Disclaimer:

  1. I haven't looked at this for a while
  2. I'm not 100% sure that this doesn't require additional licensing
  3. I'm not sure if you can do this using Python
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文