有没有办法检测您何时到达 SAS 数据步骤中的最后一个观察结果?

发布于 2024-08-05 20:25:22 字数 101 浏览 6 评论 0原文

有没有办法在运行时检查 SAS 数据集中有多少个观测值,或者检测何时到达 DATA 步骤中的最后一个观测值?

对于这个看似简单的问题,我似乎无法在网络上找到任何内容。谢谢!

Is there a way to check how many observations are in a SAS data set at runtime OR to detect when you've reached the last observation in a DATA step?

I can't seem to find anything on the web for this seemingly simple problem. Thanks!

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

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

发布评论

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

评论(4

墨洒年华 2024-08-12 20:25:22

set 语句的 nobs= 选项可以为您提供观察值的数量。编译数据步骤时,将扫描输入数据集的标头部分,因此您甚至不必执行 set 语句即可获取观测值的数量。例如,以下报告按预期报告 2:

/* a test data set with two observations and no vars */
data two;
  output;
  output;
run;

data _null_;
  if 0 then set two nobs=nobs;
  put nobs=;
run;
/* on log
nobs=2
*/

end= 选项在读入最后一个观察值(对于 set 语句)时设置一个标志。

一个 SAS 数据集,但是,可以是 SAS 数据文件或 SAS 视图。在后者的情况下,观察的数量在编译时或执行时可能是未知的。

data subclass/view=subclass;
  set sashelp.class;
  where sex = symget("sex");
run;

%let sex=F;
data girls;
  set subclass end=end nobs=nobs;
  put name= nobs= end=;
run;
/* on log
Name=Alice nobs=9.0071993E15 end=0
Name=Barbara nobs=9.0071993E15 end=0
Name=Carol nobs=9.0071993E15 end=0
Name=Jane nobs=9.0071993E15 end=0
Name=Janet nobs=9.0071993E15 end=0
Name=Joyce nobs=9.0071993E15 end=0
Name=Judy nobs=9.0071993E15 end=0
Name=Louise nobs=9.0071993E15 end=0
Name=Mary nobs=9.0071993E15 end=1
*/

The nobs= option to a set statement can give you the number of observations. When the data step is compiled, the header portion of the input datasets are scanned, so you don't even have to execute the set statement in order to get the number of observations. For instance, the following reports 2 as expected:

/* a test data set with two observations and no vars */
data two;
  output;
  output;
run;

data _null_;
  if 0 then set two nobs=nobs;
  put nobs=;
run;
/* on log
nobs=2
*/

The end= option sets a flag when the last observation (for the set statement) is read in.

A SAS data set, however, can be a SAS data file or a SAS view. In the case of the latter, the number of observations may not be known either at compile time or at execution time.

data subclass/view=subclass;
  set sashelp.class;
  where sex = symget("sex");
run;

%let sex=F;
data girls;
  set subclass end=end nobs=nobs;
  put name= nobs= end=;
run;
/* on log
Name=Alice nobs=9.0071993E15 end=0
Name=Barbara nobs=9.0071993E15 end=0
Name=Carol nobs=9.0071993E15 end=0
Name=Jane nobs=9.0071993E15 end=0
Name=Janet nobs=9.0071993E15 end=0
Name=Joyce nobs=9.0071993E15 end=0
Name=Judy nobs=9.0071993E15 end=0
Name=Louise nobs=9.0071993E15 end=0
Name=Mary nobs=9.0071993E15 end=1
*/
奈何桥上唱咆哮 2024-08-12 20:25:22

您还可以使用%sysfunc(attrn( dataset, nlobs)),尽管它仅限于 SAS 数据集(即不是数据视图)。该宏归功于这篇 SUGI 论文,其中还提供了有关以下方面的重要信息:良好的宏观设计。

您可以获得 SAS 数据集上的各种其他字符和数字信息。

请参阅有关 attrn 和 < a href="http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a000147794.htm" rel="noreferrer">attrc。

%macro numobs (data=&syslast ) ;
/* --------------------------------------------
Return number of obs as a function
--------------------------------------------
*/
%local dsid nobs rc;
%let data = &data ; /* force evaluation of &SYSLAST */
%let dsid=%sysfunc(open(&data));
%if &dsid > 0 %then
%do ;
   %let nobs=%sysfunc(attrn(&dsid,nlobs));
   %let rc=%sysfunc(close(&dsid));
%end ;
%else
   %let nobs = -1 ;
&nobs
%mend numobs;

You can also use %sysfunc(attrn( dataset, nlobs)) though it is limited to SAS data sets (i.e. not data views). Credit for the macro to this SUGI paper, which also give great information regarding good macro design.

You can get all sorts of other character and numeric information on a SAS data set.

See the documentation on attrn and attrc.

%macro numobs (data=&syslast ) ;
/* --------------------------------------------
Return number of obs as a function
--------------------------------------------
*/
%local dsid nobs rc;
%let data = &data ; /* force evaluation of &SYSLAST */
%let dsid=%sysfunc(open(&data));
%if &dsid > 0 %then
%do ;
   %let nobs=%sysfunc(attrn(&dsid,nlobs));
   %let rc=%sysfunc(close(&dsid));
%end ;
%else
   %let nobs = -1 ;
&nobs
%mend numobs;
送君千里 2024-08-12 20:25:22

查找 SAS 数据集中的观测值数量:

proc sql noprint;
  select count(*) into: nobs
  from sashelp.class
  ;
quit;

data _null_;
  put "&nobs";
run;

SQL 部分计算观测值的数量,并将该数字存储在名为“nobs”的宏变量中。
数据步骤放置要显示的数字,但您可以像使用其他变量一样使用宏变量。

处理最后一个观察时执行特定操作:

data _null_;
  set sashelp.class end=eof;
  if eof then do;
     put name= _n_=;
  end;
run;

“set”语句的“end”选项定义一个变量(此处“eof”表示文件结束),当处理最后一个观察时,该变量设置为 1。然后,您可以测试变量的值,并在其值为 1 时执行操作。有关详细信息,请参阅“set”语句的文档。

Find the number of observations in a SAS data set:

proc sql noprint;
  select count(*) into: nobs
  from sashelp.class
  ;
quit;

data _null_;
  put "&nobs";
run;

The SQL portion counts the number of observaions, and stores the number in a macro variable called "nobs".
The data step puts the number for display, but you can use the macro variable like any other.

Performing a certain action when the last observation is processed:

data _null_;
  set sashelp.class end=eof;
  if eof then do;
     put name= _n_=;
  end;
run;

The "end" option to the "set" statement defines a variable (here "eof" for end-of-file) that is set to 1 when the last observation is processed. You can then test the value of the variable, and perform actions when its value is 1. For more info, see the documentation for the "set" statement.

落花浅忆 2024-08-12 20:25:22
data hold;   
  set input_data end=last;    
    .    
    .   
    .   
  if last then do;   
    .   
    .   
    .   
  end;  
run;
data hold;   
  set input_data end=last;    
    .    
    .   
    .   
  if last then do;   
    .   
    .   
    .   
  end;  
run;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文