gpt4 book ai didi

sas - 如何在SAS中打印前10个和最后10个观察值?

转载 作者:行者123 更新时间:2023-12-04 21:14:05 25 4
gpt4 key购买 nike

我试图找到一种方法来只打印 SAS 数据集的前 10 个和最后 10 个观察值。有没有办法做到这一点?

我试过 proc print data = ia.usage; where Obs < 10 & Obs > 80;run;但该命令仍会打印出所有 90 个观察值。关于如何轻松做到这一点的任何想法?

谢谢。

最佳答案

获得前 10 个很容易:

/*First 10 obs*/
proc print data = ia.usage(obs = 10); run;

获得最后 10 个有点困难,但这可以使用 View 来完成:
/*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;

如果你想在同一个 proc 打印中,你可以创建两个 View 并将它们组合到另一个 View 中,然后打印它:
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;

即使对于大型数据集,上述方法也应该非常快,但它假定您的初始数据集不是 View ,因为它依赖于知道数据集中的行数(nobs)。如果你有一个 View ,那么你需要通读整个数据集以找出行数,然后再次阅读,丢弃除前 10 行和最后 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;

关于sas - 如何在SAS中打印前10个和最后10个观察值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46264967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com