sas - 如果表为空,如何获得等于零的行数而不是什么都没有是SAS
问题描述
我有以下代码,它将行数存储在宏变量内的表中n_vars
:
data cont_vars;
set var_list;
where flg_categorical = 0;
call symput('n_vars', _n_);
%put &n_vars;
run;
目前,如果结果表为空,则n_vars
解析为空,我希望将其设置为 0,以便%do x = 1 %to &n_vars;
稍后在循环中使用它。
解决方案
如果该where
语句确实导致在数据集中没有观察到,则以下代码应将n_vars
宏变量设置为0
:
data want;
if eof then call symputx('n_vars',_n_-1);
set have end=eof;
where flg_categorical = 0;
run;
请考虑创建一个Minimal Reproducible Example。
data have;
infile datalines delimiter=',';
input x1 flg_categorical;
datalines;
1,1
6,2
3,2
;
run;
data want;
if eof then call symputx('n_vars',_n_-1);
set have end=eof;
where flg_categorical = 0;
run;
%put &=n_vars;
结果:
无论如何,我不知道为什么你会使用%do
从1 to 0
. 当数据集为空时,您可能只想运行另一种步骤。如果是这种情况,我会考虑使用:
%if &n_vars. > 0 %then %do; /* data set is not empty */
...
%end;
%else %do; /* data set is empty */
...
%end;
推荐阅读
- javascript - Discord.js 无法读取未定义的属性“推送”
- javascript - 为什么 for...in 循环中的变量是字符串?
- c++ - 我没有在擦除到矢量之前使用删除。指针会一直占用内存吗?
- javascript - 如何在 React 的类组件中为事件监听器添加样式?
- jquery - 无法从 node_modules 导入 typed.js、jQuery 和 google-fonts
- cmake - 如何将 protobuf 与 cmake 集成?
- sql - 如何为 SQL 表自动生成数据类型
- python - 关于余弦相似度,损失函数和网络如何选择(我有两个方案)
- node.js - cannot upload csv file on nodejs server
- python - 我已经设法在我的 Debian 系统上做了一些事情,而 Python-Cryptography 不允许我安装或删除任何东西