有没有办法检测您何时到达 SAS 数据步骤中的最后一个观察结果?
有没有办法在运行时检查 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
set
语句的nobs=
选项可以为您提供观察值的数量。编译数据步骤时,将扫描输入数据集的标头部分,因此您甚至不必执行set
语句即可获取观测值的数量。例如,以下报告按预期报告 2:end=
选项在读入最后一个观察值(对于set
语句)时设置一个标志。一个 SAS 数据集,但是,可以是 SAS 数据文件或 SAS 视图。在后者的情况下,观察的数量在编译时或执行时可能是未知的。
The
nobs=
option to aset
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 theset
statement in order to get the number of observations. For instance, the following reports 2 as expected:The
end=
option sets a flag when the last observation (for theset
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.
您还可以使用
%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。
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.
查找 SAS 数据集中的观测值数量:
SQL 部分计算观测值的数量,并将该数字存储在名为“nobs”的宏变量中。
数据步骤放置要显示的数字,但您可以像使用其他变量一样使用宏变量。
处理最后一个观察时执行特定操作:
“set”语句的“end”选项定义一个变量(此处“eof”表示文件结束),当处理最后一个观察时,该变量设置为 1。然后,您可以测试变量的值,并在其值为 1 时执行操作。有关详细信息,请参阅“set”语句的文档。
Find the number of observations in a SAS data set:
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:
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.