如何打印 SAS 数据集的最后一次观察结果?

发布于 2024-12-13 05:39:47 字数 199 浏览 1 评论 0原文

我有一个包含 1000 个观察值的数据集。我只想打印最后的观察结果。使用以下内容:

proc print data=apple(firstobs = 1000 obs = 1000); 
run;

我可以获得最后的观察结果。但我必须提前知道我的数据集有 1000 个观察值。在不知道这一点的情况下我该如何做到这一点?

I have a data set with 1000 observations. I want to only print out the last observation. Using the following:

proc print data=apple(firstobs = 1000 obs = 1000); 
run;

I can get the last observation. But I have to know in advance that my data set has 1000 observations. How do I do this without knowing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪痕残 2024-12-20 05:39:47

有很多方法可以做到这一点。这里有两个:

proc sql noprint;
 select n(var1) into :nobs
 from apple;
quit;

proc print data=apple(firstobs=&nobs); run;

这只是将观察数量读取到宏变量中,然后使用它来指定第一个观察。 (请注意,var1 引用数据中的变量。)

另一种方法是创建一个仅保留最后观察结果的数据视图,然后打印该结果:

data tmp / view=tmp;
 set apple nobs=nobs;
 if _n_=nobs;
run;

proc print data=tmp; run;

There are many ways you could do this. Here are two:

proc sql noprint;
 select n(var1) into :nobs
 from apple;
quit;

proc print data=apple(firstobs=&nobs); run;

This just reads the number of observations into a macro variable, and then use that to specify the first observation. (Note that var1 refers to a variable in your data.)

Another approach would be to create a data view that only keeps the last observation and then print that:

data tmp / view=tmp;
 set apple nobs=nobs;
 if _n_=nobs;
run;

proc print data=tmp; run;
清秋悲枫 2024-12-20 05:39:47

我认为 SETMERGEMODIFYUPDATEend 选项> 声明非常有用。

data x;
  do i = 1 to 1000;
    output;
  end;
run;

data x;
  set x end = _end;
  end = _end;
proc print data = x;
  where end;
run;

I think that the end option for the SET, MERGE, MODIFY, or UPDATE statement is very useful.

data x;
  do i = 1 to 1000;
    output;
  end;
run;

data x;
  set x end = _end;
  end = _end;
proc print data = x;
  where end;
run;

有很多方法可以找到观测值的数量;以下宏是一个示例。

%macro nobs (dsn);
   %let nobs=0;
   %let dsid = %sysfunc(open(&dsn));
   %if &dsid %then %let nobs = %sysfunc(attrn(&dsid,nobs));
   %let rc   = %sysfunc(close(&dsid));
   &nobs
%mend nobs;

%let n = %nobs(apple);

proc print data=apple (firstobs=&n obs=&n); run;

There are many ways to find the number of observations; the following macro is one example.

%macro nobs (dsn);
   %let nobs=0;
   %let dsid = %sysfunc(open(&dsn));
   %if &dsid %then %let nobs = %sysfunc(attrn(&dsid,nobs));
   %let rc   = %sysfunc(close(&dsid));
   &nobs
%mend nobs;

%let n = %nobs(apple);

proc print data=apple (firstobs=&n obs=&n); run;
那支青花 2024-12-20 05:39:47

有两种简单的解决方案:

解决方案 1:

data result;
    set apple end=end;
    if end then output;
run;
proc print data=result;
run;

解决方案 2:

data result;
    set apple nobs=nobs;
    if _N_=nobs then output;
run;
proc print data=result;
run;

There are two simple solutions:

Solution 1:

data result;
    set apple end=end;
    if end then output;
run;
proc print data=result;
run;

Solution 2:

data result;
    set apple nobs=nobs;
    if _N_=nobs then output;
run;
proc print data=result;
run;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文