【发布时间】:2018-11-02 20:22:00
【问题描述】:
如何将我的 SAS 数据集转换为可以轻松粘贴到论坛或移交给他人以复制我的数据的数据集。理想情况下,我还希望能够控制包含的记录数量。
即我在 SASHELP 库中有 sashelp.class,但我想在此处提供它,以便其他人可以将其用作我的问题的起点。
【问题讨论】:
标签: sas
如何将我的 SAS 数据集转换为可以轻松粘贴到论坛或移交给他人以复制我的数据的数据集。理想情况下,我还希望能够控制包含的记录数量。
即我在 SASHELP 库中有 sashelp.class,但我想在此处提供它,以便其他人可以将其用作我的问题的起点。
【问题讨论】:
标签: sas
为此,您可以使用 Mark Jordan 在 SAS 编写的宏,代码也存储在 GitHub 中。
您需要提供数据集名称,包括库和要输出的观察数。它按顺序排列它们。然后代码将出现在您的 SAS 日志中。
*data set you want to create demo data for;
%let dataSetName = sashelp.Class;
*number of observations you want to keep;
%let obsKeep = 5;
******************************************************
DO NOT CHANGE ANYTHING BELOW THIS LINE
******************************************************;
%let source_path = https://gist.githubusercontent.com/statgeek/bcc55940dd825a13b9c8ca40a904cba9/raw/865d2cf18f5150b8e887218dde0fc3951d0ff15b/data2datastep.sas;
filename reprex url "&source_path";
%include reprex;
filename reprex;
option linesize=max;
%data2datastep(dsn=&dataSetName, obs=&obsKeep);
如果您无权访问 github 页面,这可能不起作用,在这种情况下,您可以手动导航到该页面(相同链接)并将其复制/粘贴到 SAS 中。然后运行程序,只运行最后一步,%data2datastep(dsn=, obs=);
【讨论】:
这个话题最近出现在 SAS Communities 上,我创建了一个比 Reeza 链接的宏更强大的宏。你可以在 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 个观察结果,您可以运行以下宏调用:
%ds2post(sashelp.cars,obs=5)
这会在 SAS 日志中生成此代码:
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
;;;;
试试这个小测试来比较两个宏。
首先制作一个包含几个问题的示例数据集。
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 日志。
%ds2post(testit);
%data2datastep(dsn=testit,obs=20);
从日志中复制代码。更改 DATA 语句中的名称以不覆盖原始数据集或相互覆盖。运行它们并将结果与原始结果进行比较。
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
请注意,我确信有些值也会给我的宏带来麻烦。但希望它们是由比空格或分号更不可能出现的数据引起的。
【讨论】: