将SAS数据(包括表结构)存储在单个平面文件中

时间:2022-12-10 16:57:05

I need to convert SAS data tables into flat files (or "ASCII files" as they were called once, as opposed to binary files). And only one flat file for each original SAS table. The challenging thing is that I want the flat file to contain some structural information of the original SAS table also, specifically:

我需要将SAS数据表转换为平面文件(或“ASCII文件”,因为它们被调用一次,而不是二进制文件)。每个原始SAS表只有一个平面文件。具有挑战性的是我希望平面文件也包含原始SAS表的一些结构信息,具体来说:

  • Variable/Column name
  • Variable/Column label
  • Variable/Column type
  • Variable/Column length
  • Variable/Column format
  • Variable/Column informat

Additional information:

  • I will only need to convert small data (< 100 obs).
  • 我只需要转换小数据(<100 obs)。

  • Performance is not an issue (within reasonable limits).
  • 性能不是问题(在合理的范围内)。

  • The flat file should form a basis for recreating the original SAS table, I don't need to be able to use the file directly as a table in DATA or PROC steps.
  • 平面文件应该构成重新创建原始SAS表的基础,我不需要能够直接将该文件用作DATA或PROC步骤中的表。

The standard SAS tables, transport files, XPORT files, etc are all binary format files, and the standard XML table format in SAS and CSV-files don't preserve table structure. So obviously these options don't help.

标准SAS表,传输文件,XPORT文件等都是二进制格式文件,SAS和CSV文件中的标准XML表格格式不保留表结构。显然这些选项没有帮助。

What is my best option?

什么是我最好的选择?

6 个解决方案

#1


4  

I'm not aware of any easy solutions.

我不知道任何简单的解决方案。

Possibly:

  1. Use PROC EXPORT to produce CSV file with the data in it.
  2. 使用PROC EXPORT生成包含数据的CSV文件。

  3. Use PROC DATASETS with ODS to produce a dataset with the names, types, etc.
  4. 将PROC DATASETS与ODS一起使用以生成包含名称,类型等的数据集。

  5. Produce another CSV file for this dataset.
  6. 为此数据集生成另一个CSV文件。

Now you've got your ASCII description of the table (spread over two CSV files). Reversing the process would be more tricky. Basically you'd have to read in the description data set, then use CALL SYMPUT in a loop to create a bunch of macro variables with the information in them, then use your macro variables to build a PROC IMPORT for the CSV file...

现在您已经获得了表格的ASCII描述(分布在两个CSV文件中)。扭转这个过程会更棘手。基本上你必须在描述数据集中读取,然后在循环中使用CALL SYMPUT创建一堆包含其中信息的宏变量,然后使用宏变量为CSV文件构建PROC IMPORT ...

#2


3  

  1. Create the code to export the table to text (this is straightforward, just google it or look at 'The Little SAS Book' if you have a copy).

    创建代码以将表格导出为文本(这很简单,只需google它或者如果您有副本,请查看'The Little SAS Book')。

  2. Then append the 'meta' info from sashelp.vcolumn, which is where sas stores information (meta data) about sas datasets. It's a sas table itself, so you could do a proc sql union operation to join it with the actual columns that this table describes (though you will need to do a transpose type operation because the meta data about the columns is in rows, not columns).

    然后附加来自sashelp.vcolumn的'meta'信息,sashelp.vcolumn是sas存储有关sas数据集的信息(元数据)的地方。它本身就是一个sas表,所以你可以做一个proc sql union操作来将它与这个表描述的实际列连接起来(虽然你需要做一个转置类型操作,因为有关列的元数据是行,而不是列)。

You're not being completely specific about how you want to see the meta data in the text file, so that's as far as I can go.

你并没有完全具体地说明你希望如何在文本文件中看到元数据,所以这就是我可以去的地方。

#3


2  

proc sql's describe syntax might be handy to get the metadata portion, including lengths, types, formats, indexes etc...

proc sql的describe语法可能很方便获取元数据部分,包括长度,类型,格式,索引等...

Code:

proc sql;
describe table sashelp.class;
quit;

Log:

NOTE: SQL table SASHELP.CLASS was created like:

create table SASHELP.CLASS( bufsize=4096 )
  (
   Name char(8),
   Sex char(1),
   Age num,
   Height num,
   Weight num
  );

#4


1  

With SAS 9.2, you can create an XML file from a data set and the XML contains variable/column metadata, like format, label, etc... See the section of the SAS 9.2 XML LIBNAME Engine: User's Guide titled "Using the XML Engine to Transport SAS Data Sets across Operating Environments". A link to it is here:

使用SAS 9.2,您可以从数据集创建XML文件,XML包含变量/列元数据,如格式,标签等...请参阅“SAS 9.2 XML LIBNAME引擎:用户指南”中标题为“使用XML”的部分引擎以跨操作环境传输SAS数据集“。它的链接在这里:

http://support.sas.com/documentation/cdl/en/engxml/61740/HTML/default/a002594382.htm

Here's a section of code from the manual that shows using the XML92 libname engine and PROC COPY to create the XML:

以下是手册中的一段代码,其中显示了使用XML92 libname引擎和PROC COPY来创建XML:

libname myfiles 'SAS-library';
libname trans xml92 'XML-document' xmltype=export;
proc copy in=myfiles out=trans;
   select class;
run;

In SAS 9.1.3, you may have to create a custom tagset to get the same operation. SAS Technical Support (support@sas.com) may be able to offer some help.

在SAS 9.1.3中,您可能必须创建自定义标记集才能获得相同的操作。 SAS技术支持(support@sas.com)可能会提供一些帮助。

#5


0  

BTW - you haven't said why you need to do this. In this case, there is no good reason (there might be a compelling reason, such as somebody with power saying 'do it, or be fired', but there's no good reason).

顺便说一句 - 你还没有说明为什么你需要这样做。在这种情况下,没有充分的理由(可能有一个令人信服的理由,例如有权力的人说'做它,或被解雇',但没有充分的理由)。

I'd give up the idea of merging the metadata and data in each file, unless there's some incredibly strong reason to do so. Go with exporting the metadata for data set A into a file called metadata_A; this will result in paired files. Anybody looking to use those files in a a database program or statistical program would have a clearly-labeled metadata file to work with.

我放弃了在每个文件中合并元数据和数据的想法,除非有一些令人难以置信的强有力的理由这样做。将数据集A的元数据导出到名为metadata_A的文件中;这将导致配对文件。任何想要在数据库程序或统计程序中使用这些文件的人都可以使用清晰标记的元数据文件。

#6


-1  

If you're only going to use the data in SAS, then you can just use PROC COPY to make transport files:

如果您只想使用SAS中的数据,那么您只需使用PROC COPY来制作传输文件:

http://www.usc.edu/isd/doc/statistics/sas/sastransport/

#1


4  

I'm not aware of any easy solutions.

我不知道任何简单的解决方案。

Possibly:

  1. Use PROC EXPORT to produce CSV file with the data in it.
  2. 使用PROC EXPORT生成包含数据的CSV文件。

  3. Use PROC DATASETS with ODS to produce a dataset with the names, types, etc.
  4. 将PROC DATASETS与ODS一起使用以生成包含名称,类型等的数据集。

  5. Produce another CSV file for this dataset.
  6. 为此数据集生成另一个CSV文件。

Now you've got your ASCII description of the table (spread over two CSV files). Reversing the process would be more tricky. Basically you'd have to read in the description data set, then use CALL SYMPUT in a loop to create a bunch of macro variables with the information in them, then use your macro variables to build a PROC IMPORT for the CSV file...

现在您已经获得了表格的ASCII描述(分布在两个CSV文件中)。扭转这个过程会更棘手。基本上你必须在描述数据集中读取,然后在循环中使用CALL SYMPUT创建一堆包含其中信息的宏变量,然后使用宏变量为CSV文件构建PROC IMPORT ...

#2


3  

  1. Create the code to export the table to text (this is straightforward, just google it or look at 'The Little SAS Book' if you have a copy).

    创建代码以将表格导出为文本(这很简单,只需google它或者如果您有副本,请查看'The Little SAS Book')。

  2. Then append the 'meta' info from sashelp.vcolumn, which is where sas stores information (meta data) about sas datasets. It's a sas table itself, so you could do a proc sql union operation to join it with the actual columns that this table describes (though you will need to do a transpose type operation because the meta data about the columns is in rows, not columns).

    然后附加来自sashelp.vcolumn的'meta'信息,sashelp.vcolumn是sas存储有关sas数据集的信息(元数据)的地方。它本身就是一个sas表,所以你可以做一个proc sql union操作来将它与这个表描述的实际列连接起来(虽然你需要做一个转置类型操作,因为有关列的元数据是行,而不是列)。

You're not being completely specific about how you want to see the meta data in the text file, so that's as far as I can go.

你并没有完全具体地说明你希望如何在文本文件中看到元数据,所以这就是我可以去的地方。

#3


2  

proc sql's describe syntax might be handy to get the metadata portion, including lengths, types, formats, indexes etc...

proc sql的describe语法可能很方便获取元数据部分,包括长度,类型,格式,索引等...

Code:

proc sql;
describe table sashelp.class;
quit;

Log:

NOTE: SQL table SASHELP.CLASS was created like:

create table SASHELP.CLASS( bufsize=4096 )
  (
   Name char(8),
   Sex char(1),
   Age num,
   Height num,
   Weight num
  );

#4


1  

With SAS 9.2, you can create an XML file from a data set and the XML contains variable/column metadata, like format, label, etc... See the section of the SAS 9.2 XML LIBNAME Engine: User's Guide titled "Using the XML Engine to Transport SAS Data Sets across Operating Environments". A link to it is here:

使用SAS 9.2,您可以从数据集创建XML文件,XML包含变量/列元数据,如格式,标签等...请参阅“SAS 9.2 XML LIBNAME引擎:用户指南”中标题为“使用XML”的部分引擎以跨操作环境传输SAS数据集“。它的链接在这里:

http://support.sas.com/documentation/cdl/en/engxml/61740/HTML/default/a002594382.htm

Here's a section of code from the manual that shows using the XML92 libname engine and PROC COPY to create the XML:

以下是手册中的一段代码,其中显示了使用XML92 libname引擎和PROC COPY来创建XML:

libname myfiles 'SAS-library';
libname trans xml92 'XML-document' xmltype=export;
proc copy in=myfiles out=trans;
   select class;
run;

In SAS 9.1.3, you may have to create a custom tagset to get the same operation. SAS Technical Support (support@sas.com) may be able to offer some help.

在SAS 9.1.3中,您可能必须创建自定义标记集才能获得相同的操作。 SAS技术支持(support@sas.com)可能会提供一些帮助。

#5


0  

BTW - you haven't said why you need to do this. In this case, there is no good reason (there might be a compelling reason, such as somebody with power saying 'do it, or be fired', but there's no good reason).

顺便说一句 - 你还没有说明为什么你需要这样做。在这种情况下,没有充分的理由(可能有一个令人信服的理由,例如有权力的人说'做它,或被解雇',但没有充分的理由)。

I'd give up the idea of merging the metadata and data in each file, unless there's some incredibly strong reason to do so. Go with exporting the metadata for data set A into a file called metadata_A; this will result in paired files. Anybody looking to use those files in a a database program or statistical program would have a clearly-labeled metadata file to work with.

我放弃了在每个文件中合并元数据和数据的想法,除非有一些令人难以置信的强有力的理由这样做。将数据集A的元数据导出到名为metadata_A的文件中;这将导致配对文件。任何想要在数据库程序或统计程序中使用这些文件的人都可以使用清晰标记的元数据文件。

#6


-1  

If you're only going to use the data in SAS, then you can just use PROC COPY to make transport files:

如果您只想使用SAS中的数据,那么您只需使用PROC COPY来制作传输文件:

http://www.usc.edu/isd/doc/statistics/sas/sastransport/