SAS中文论坛

标题: 这段程序怎么写,对我这种低手来说是太难了 [打印本页]

作者: shiyiming    时间: 2005-1-4 22:17
标题: 这段程序怎么写,对我这种低手来说是太难了
数据如下:
id action Amount
1 into 100
2 into 20
2 out 20
2 into 30
2 out 30
3 into 200
3 out 200
3 into 2000
4 into 1
这个表记录每个id的历史情况,如:id 1曾经into,金额是100,至今未出去;id 2曾经into,金额20,后又out,第二次into,金额30,后out......
注意:上面数据已按id和action的日期排序了(date字段未列出)
我想得到下面的数据,每个id的每一个into/out周期都保留下来,生成如下结果(.表示missing)
======================================
id action1 amount1 action2 amount2
1 into 100 . .
2 into 20 out 20
2 into 30 out 30
3 into 200 out 200
3 into 2000 . .
4 into 1 . .

请高人帮忙!!
作者: shiyiming    时间: 2005-1-21 10:34
标题: 我的思路
我也是新手,一点思路不知道对不对
可以试试按照action来取子集,
然后merge data的时候按照id把各个action和value并入.
作者: shiyiming    时间: 2005-1-26 10:04
标题: 这个是否满足要求
这个是否满足要求
作者: shiyiming    时间: 2005-2-28 19:32
标题: 回复!
设原数据为t1,t6为要得到的数据集!我试过了,可以!
[code:8f0bd]proc sort data=t1;
by id;
run;
data t2 t3;
set t1;
if action='into' then output t2;
else output t3;
run;
data t4;
rename action=action1 amount=amount1;
set t2;
by id;
if first.id then tag=1;
else tag+1;
run;
data t5;
rename action=action2 amount=amount2;
set t3;
by id;
if first.id then tag=1;
else tag+1;
run;
proc sort data=t4;
by id tag;
run;
proc sort data=t5;
by id tag;
run;
data t6(drop=tag);
merge t4 t5;
by id tag;
run;[/code:8f0bd]




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