如何将 SAS 数据集转换为数据步骤 [英] How to convert a SAS data set to a data step

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

问题描述

我如何将我的 SAS 数据集转换成一个数据集,以便我可以轻松地将其粘贴到论坛中或交给其他人来复制我的数据.理想情况下,我还希望能够控制包含的记录数量.

How can I convert my SAS data set, into a data set that I can easily paste into the forum or hand over to someone to replicate my data. Ideally, I'd also like to be able to control the amount of records that are included.

即我在 SASHELP 库中有 sashelp.class,但我想在这里提供它,以便其他人可以使用它作为我问题的起点.

Ie I have sashelp.class in the SASHELP library, but I want to provide it here so others can use it as the starting point for my question.

推荐答案

这个主题最近出现在 SAS Communities 上,我创建了一个比 Reeza 链接的宏更强大的宏.你可以在 Github 中看到它:ds2post.sas

This topic came up recently on SAS Communities and I created a little more robust macro than the one Reeza linked. You can see it in Github: ds2post.sas

* Pull macro definition from GITHUB ;
filename ds2post url
  'https://raw.githubusercontent.com/sasutils/macros/master/ds2post.sas'
;
%include ds2post ;

例如,如果您想分享 SASHELP.CARS 的前 5 个观察结果,您可以运行此宏调用:

For example if you wanted to share the first 5 observations of SASHELP.CARS you would run this macro call:

%ds2post(sashelp.cars,obs=5)

这会在 SAS 日志中生成此代码:

Which would generate this code to the SAS log:

data work.cars (label='2004 Car Data');
  infile datalines dsd dlm='|' truncover;
  input Make :$13. Model :$40. Type :$8. Origin :$6. DriveTrain :$5.
    MSRP Invoice EngineSize Cylinders Horsepower MPG_City MPG_Highway
    Weight Wheelbase Length
  ;
  format MSRP dollar8. Invoice dollar8. ;
  label EngineSize='Engine Size (L)' MPG_City='MPG (City)'
    MPG_Highway='MPG (Highway)' Weight='Weight (LBS)'
    Wheelbase='Wheelbase (IN)' Length='Length (IN)'
  ;
datalines4;
Acura|MDX|SUV|Asia|All|36945|33337|3.5|6|265|17|23|4451|106|189
Acura|RSX Type S 2dr|Sedan|Asia|Front|23820|21761|2|4|200|24|31|2778|101|172
Acura|TSX 4dr|Sedan|Asia|Front|26990|24647|2.4|4|200|22|29|3230|105|183
Acura|TL 4dr|Sedan|Asia|Front|33195|30299|3.2|6|270|20|28|3575|108|186
Acura|3.5 RL 4dr|Sedan|Asia|Front|43755|39014|3.5|6|225|18|24|3880|115|197
;;;;

试试这个小测试来比较两个宏.

Try this little test to compare the two macros.

首先制作一个有几个问题的示例数据集.

First make a sample dataset with a couple of issues.

data testit;
  set sashelp.class (obs=5);
  if _n_=1 then name='Le Bron';
  if _n_=2 then age=.;
  if _n_=3 then wt=.;
  if _n_=4 then name='12;34';
run;

然后运行两个宏将代码转储到 SAS 日志.

Then run both macros to dump code to the SAS log.

%ds2post(testit);
%data2datastep(dsn=testit,obs=20);

从日志中复制代码.更改 DATA 语句中的名称以不覆盖原始数据集或相互覆盖.运行它们并将结果与​​原始结果进行比较.

Copy the code from the log. Changing the name in the DATA statements to not overwrite the original dataset or each other. Run them and compare the result to the original.

proc compare data=testit compare=testit1; run;
proc compare data=testit compare=testit2; run;

使用 %DS2POST 的结果:

The COMPARE Procedure
Comparison of WORK.TESTIT with WORK.TESTIT1
(Method=EXACT)

Data Set Summary

Dataset                Created          Modified  NVar    NObs

WORK.TESTIT   02NOV18:17:09:40  02NOV18:17:09:40     6       5
WORK.TESTIT1  02NOV18:17:10:29  02NOV18:17:10:29     6       5

Variables Summary

Number of Variables in Common: 6.

Observation Summary

Observation      Base  Compare

First Obs           1        1
Last  Obs           5        5

Number of Observations in Common: 5.
Total Number of Observations Read from WORK.TESTIT: 5.
Total Number of Observations Read from WORK.TESTIT1: 5.

Number of Observations with Some Compared Variables Unequal: 0.
Number of Observations with All Compared Variables Equal: 5.

使用 %Data2DataStep 的结果摘要:

Comparison of WORK.TESTIT with WORK.TESTIT2
(Method=EXACT)

Data Set Summary

Dataset                Created          Modified  NVar    NObs

WORK.TESTIT   02NOV18:17:09:40  02NOV18:17:09:40     6       5
WORK.TESTIT2  02NOV18:17:10:29  02NOV18:17:10:29     6       3


Variables Summary

Number of Variables in Common: 6.


Observation Summary

Observation      Base  Compare

First Obs           1        1
First Unequal       1        1
Last  Unequal       3        3
Last  Match         3        3
Last  Obs           5        .

Number of Observations in Common: 3.
Number of Observations in WORK.TESTIT but not in WORK.TESTIT2: 2.
Total Number of Observations Read from WORK.TESTIT: 5.
Total Number of Observations Read from WORK.TESTIT2: 3.

Number of Observations with Some Compared Variables Unequal: 3.
Number of Observations with All Compared Variables Equal: 0.

变量值汇总

Values Comparison Summary

Number of Variables Compared with All Observations Equal: 1.
Number of Variables Compared with Some Observations Unequal: 5.
Number of Variables with Missing Value Differences: 4.
Total Number of Values which Compare Unequal: 12.
Maximum Difference: 0.


Variables with Unequal Values

Variable  Type  Len  Ndif   MaxDif  MissDif

Name      CHAR    8     1                 0
Sex       CHAR    1     3                 3
Age       NUM     8     2        0        2
Height    NUM     8     3        0        3
Weight    NUM     8     3        0        3

请注意,我确信有些值也会给我的宏带来麻烦.但希望它们是由比空格或分号更不可能出现的数据引起的.

Note that I am sure there are values that will cause trouble for my macro also. But hopefully they are caused by data that is less likely to occur than spaces or semi-colons.

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

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