在R中,您可以使用all()或any()函数在列变量中的所有行上执行条件.SAS中是否有等效的方法?
如果列x中的任何行都是负数,我想要条件,这应该返回TRUE.
或者,如果列y中的所有行都是负数,则应返回TRUE.
例如
x y
-1 -2
2 -4
3 -4
4 -3
Run Code Online (Sandbox Code Playgroud)
在R:
all(x<0) 将输出FALSEall(y<0) 将输出为TRUE我希望在SAS中复制相同的列式操作.
为了完整起见,这里是SAS-IML解决方案.当然,它是微不足道的,因为any和all函数存在同名...我还包括一个使用loc来识别正元素的例子.
data have ;
input x y @@;
cards;
1 2
2 4
3 -4
4 -3
;
run;
proc iml;
use have;
read all var {"x" "y"};
print x y;
x_all = all(x>0);
x_any = any(x>0);
y_all = all(y>0);
y_any = any(y>0);
y_pos = y[loc(y>0)];
print x_all x_any y_all y_any;
print y_pos;
quit;
Run Code Online (Sandbox Code Playgroud)
如果您想使用SQL汇总函数操作最容易做的所有观察.
SAS将布尔表达式评估为1表示true,0表示false表示布尔表达式.因此,要确定是否有任何观察条件,您要测试是否MAX( condition )为真(即等于1).要确定所有观察是否具有您想要测试的条件是否MIN( condition )为真.
data have ;
input x y @@;
cards;
-1 -2 2 -4 3 -4 4 -3
;
proc sql ;
create table want as
select
min(x<0) as ALL_X
, max(x<0) as ANY_X
, min(y<0) as ALL_Y
, max(y<0) as ANY_Y
from have
;
quit;
Run Code Online (Sandbox Code Playgroud)
结果
Obs ALL_X ANY_X ALL_Y ANY_Y
1 0 1 1 1
Run Code Online (Sandbox Code Playgroud)
SQL可能是最类似感觉的类似方式,但数据步骤同样有效,并且适合任何类型的修改 - 坦率地说,如果你正在尝试学习SAS,可能就是简单地从学习如何做SAS方式的角度出发.
data want;
set have end=eof;
retain any_x all_x; *persist the value across rows;
any_x = max(any_x, (x>0)); *(x>0) 1=true 0=false, keep the MAX (so keep any true);
all_x = min(all_x, (x>0)); *(x>0) keep the MIN (so keep any false);
if eof then output; *only output the final row to a dataset;
*and/or;
if eof then do; *here we output the any/all values to macro variables;
call symputx('any_x',any_x); *these macro variables can further drive logic;
call symputx('all_x',all_x); *and exist in a global scope (unless we define otherwise);
end;
run;
Run Code Online (Sandbox Code Playgroud)