SAS中文论坛

标题: 关于数据计算时遇到的取舍问题 [打印本页]

作者: shiyiming    时间: 2005-2-3 14:34
标题: 关于数据计算时遇到的取舍问题
可能问题太简单了,但还是希望大侠能够帮助指点一下
有n个变量,要对其倒数的平方求和,如果其中有变量为零的话,无法进行求和或者取log,怎么统计出非0的变量数(richness)?
例如:
index=1/(a*a)+1/(b*b)+1/(c*c)+.....
或者index=log(a)+log(b)+log(c).....
假设b为0,或者b和c为0.
怎么让函数计算时舍去这个部分.
能否像excel那样写成IF(B3>0,LOG(B3))+IF(C3>0,LOG(C3).....?
假设我的数据是这样的:
a        b        c        d        e        f
20        0        30        50        4        5
10        10        0        50        4        5
5        10        30        0        4        5
4        10        30        50        0        5
2        10        30        50        4        5
1        10        30        50        4        0
7        0        30        0        4        0
8        10        30        50        4        5
4        0        30        0        4        5
10        10        30        50        0        5
20        10        30        0        4        5
10        10        30        50        4        5
20        10        30        50        0        5
30        10        0        50        4        5
作者: shiyiming    时间: 2005-2-23 17:07
标题: 回复
1. 如果有1个变量=0就删除这一行数据,可以用
data a1;set aa(原数据集);
if a=0 or b=0 or c=0 or d=0 or e=0 then delete;

2. 如果变量=0则变为缺失值,不参与计算,则可以用
if a>0 then a1=1/(a*a);else a1=.;
if b>0 then b1=1/(b*b);else b1=.;
...
if f>0 then f1=1/(f*f); else f1=.;
index=sum(of a1-f1);

可能这办法有点笨。
作者: shiyiming    时间: 2005-2-28 18:55
标题: 借助数组!
可以借助数组来实现!
为使用方便,把变量名改为x1,x2,~,x5,设temp为初始数据集
data temp1;
set temp;
array x{5} x1-x5;
y=0;
do i=1 to 5;
if x(i)=0 then x(i)=.;
y+(1/x(i))^2;
end;
run;




欢迎光临 SAS中文论坛 (http://mysas.net/forum/) Powered by Discuz! X3.2