Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SAS中打印前10次和最后10次观察结果?_Sas_Statistics - Fatal编程技术网

如何在SAS中打印前10次和最后10次观察结果?

如何在SAS中打印前10次和最后10次观察结果?,sas,statistics,Sas,Statistics,我试图找到一种方法,只打印SAS数据集的前10个和最后10个观察值。我有办法做到这一点吗 我尝试了proc print data=ia.usage;其中Obs80;运行 但该命令仍然打印出所有90个观测值。你知道如何轻松做到这一点吗 谢谢。获得前10名很容易: /*First 10 obs*/ proc print data = ia.usage(obs = 10); run; 获取最后10个有点困难,但这可以使用视图完成: /*Last 10 obs*/ data last10 /view

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

我尝试了
proc print data=ia.usage;其中Obs<10&Obs>80;运行
但该命令仍然打印出所有90个观测值。你知道如何轻松做到这一点吗


谢谢。

获得前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行和后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;
data first10\u last10/view=first10\u last10;
do nobs=1乘1直到(eof);
设置ia.usage(drop=\u all\u)end=eof/*我们只需要行计数,所以在这个过程中删除所有变量以节省一点时间*/
结束;
do=1至nobs;
设置ia.usage;
如果n=nobs-9,则输出;
结束;
跑
proc print data=first10\u last10;

这可以使用单个视图实现:

data want/view=want;
  set ia.usage nobs=__nobs;
  if _n_ le 10 or _n_ gt __nobs-10;
run;

proc print data=want;
run;

对于大型数据集,这可能会很慢,因为它需要读取整个数据集,而不仅仅是前/后10行。是的,同意,您提交的方法更有效