全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1261 6
2012-01-31
现在有一个数据是这样的
QZ H1 F1 F2 F3 F4
1 1 001 1 0 2 15
1 1 001 2 1 1 12
1 1 001 3 2 1 1
1 1 002 1 0 2 3
1 1 002 2 1 1 5
1 1 003 1 0 1 7
1 1 004 1 0 2 13
1 1 004 2 3 1 34
1 004 3 5 1 2


H1未分组数据
现在要在每组内进行运算
让F2不为0的数字所对应的F4分别减去F2=0所对应的F4的值。如第一组中(12-15),(1-15)~~~~
希望SAS高手帮忙
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2012-1-31 22:11:19
That's my solution.

data test01(drop = f4_with_f2_eq_0)
          test02(keep = h1 f4_with_f2_eq_0);
        format h1 $10.;
        input Q Z H1 $ F1 F2 F3 F4;
        if f2 = 0 then do;
                f4_with_f2_eq_0 = f4;
                output test02;
        end;else output test01;
cards;
1 1 001 1 0 2 15
1 1 001 2 1 1 12
1 1 001 3 2 1 1
1 1 002 1 0 2 3
1 1 002 2 1 1 5
1 1 003 1 0 1 7
1 1 004 1 0 2 13
1 1 004 2 3 1 34
1 1 004 3 5 1 2
;run;
data test03;
        if 0 then set test02;
        if _n_ = 1 then do;
                dcl hash h(dataset:"test02");
                h.definekey("h1");
                h.definedata("f4_with_f2_eq_0");
                h.definedone();
        end;
        set test01;
        if h.find()=0 then do;
                new_f4 = f4 - f4_with_f2_eq_0;
        end;
run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-2-1 08:32:26
简单语句的

data a;
input Q Z H1 $3. F1 F2 F3 F4;
cards;
1 1 001 1 0 2 15
1 1 001 2 1 1 12
1 1 001 3 2 1 1
1 1 002 1 0 2 3
1 1 002 2 1 1 5
1 1 003 1 0 1 7
1 1 004 1 0 2 13
1 1 004 2 3 1 34
1 1 004 3 5 1 2
;
run;

proc sql;
create table b as
select a.*,b.f4 as f5
from a left join a as b
on a.h1=b.h1 and b.f2=0;
quit;

data b;
set b;
f6=f4-f5;
run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-2-1 09:39:06
学习了~~~
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-2-1 10:14:13
用一下 “藤椅”楼建立的原数据a:
data res;
set a;
by h1;
retain f2_0;
if first.h1 then f2_0=f4;
res=f4-f2_0;
run;
不过之前最好sort by h1 f2 并且确认每组h1都有f2=0唯一项~
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-2-1 18:18:56
复制代码
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群