我正在尝试找到一种只打印SAS数据集的前10个和最后10个观测值的方法。有没有办法让我这么做?
我尝试了proc print data = ia.usage; where Obs < 10 & Obs > 80;run;,但该命令仍然打印出所有90个观察结果。有没有办法轻松做到这一点?
谢谢。
发布于 2017-09-17 23:18:18
获取前10个很容易:
/*First 10 obs*/
proc print data = ia.usage(obs = 10); run;获取最后10个有点困难,但这可以使用视图来完成:
/*Last 10 obs*/
data last10 /view = last10;
startobs = nobs - 9;
set ia.usage nobs = nobs firstobs = startobs;
drop startobs;
run;
proc print data = last10; run;如果您希望在同一过程中打印这两个视图,您可以创建两个视图并将它们合并到另一个视图中,然后打印它:
data first10 /view = first10;
set ia.usage(obs = 10);
run;
data first10_last10 /view = first10_last10;
set first10 last10;
run;
proc print data = first10_last10; run;上面的方法应该非常快,即使对于大型数据集,但它假设您的初始数据集不是视图,因为它依赖于知道数据集中的行数(Nob)。如果您有一个视图,那么您将需要读取整个数据集以找出行数,然后再次读取它,丢弃除第一行和最后10行之外的所有内容。这将会慢得多。例如。
data first10_last10 /view = first10_last10;
do nobs = 1 by 1 until(eof);
set ia.usage(drop = _all_) end = eof; /*We only want the row count, so drop all vars on this pass to save a bit of time*/
end;
do _n_ = 1 to nobs;
set ia.usage;
if _n_ <= 10 or _n_ >= nobs - 9 then output;
end;
run;
proc print data = first10_last10;发布于 2017-09-17 23:24:14
这可以使用单个视图来实现:
data want/view=want;
set ia.usage nobs=__nobs;
if _n_ le 10 or _n_ gt __nobs-10;
run;
proc print data=want;
run;https://stackoverflow.com/questions/46264967
复制相似问题