SAS中文论坛

标题: 数据集删除观测问题 [打印本页]

作者: shiyiming    时间: 2010-8-26 07:44
标题: 数据集删除观测问题
情况如下:
数据集a ,可能包含数据集b中的所有观测;
若a ,b中都有相同的观测(相同的id),新变量h=1(else h=0);
[code:1oi0erul]
data b;
input m id;
cards;
2        303481
1        303349
2        321002
2        303430
4        303553
1        303389
1        313001
1        313001
2        303406
1        328003
4        303263
1        302276
2        303678
2        302631
1        303034
2        316007
1        303658
2        302770
2        303665
2        303680
2        314000
2        302867
2        328005
2        302008
2        303699
2        321005
2        321019
1        303432
1        303523
2        303199
2        303519
;
[/code:1oi0erul]
[code:1oi0erul]
data a;
input  m id;
cards;
2        302783
2        303159
1        303163
4        303190
1        303209
1        303225
2        303306
1        302468
2        302866
1        302978
2        303189
2        303189
2        303495
1        302680
2        302704
2        302729
2        302918
1        303159
2        303172
2        303263
3        303407
2        302859
1        302868
2        302880
1        303014
1        303051
2        303109
2        303146
2        303219
2        303232
1        303235
1        302805
2        303036
2        303157
2        303179
2        303189
2        302293
1        302342
2        302509
2        302931
2        303230
1        302842
2        302895
1        302932
1        303005
2        303032
1        303036
1        303123
1        303251
1        303379
4        302197
1        302905
1        303095
4        302688
2        302911
2        302941
2        302983
1        302995
1        303021
2        303025
1        303222
;
[/code:1oi0erul]
作者: shiyiming    时间: 2010-8-27 22:59
标题: Re: 数据集删除观测问题
大伙帮帮忙,看似不难,但是却阻止了我的工作,好期待有效回复!!!
作者: shiyiming    时间: 2010-8-28 11:37
标题: Re: 数据集删除观测问题
[code:37011cvm]proc sql;
    create table c as
        select a.*,
               case b.h
                    when 1 then 1
                    else 0
               end as h
            from a left join (select distinct id, 1 as h from b) b
                on a.id=b.id;
quit;[/code:37011cvm]
作者: shiyiming    时间: 2010-8-29 21:06
标题: Re: 数据集删除观测问题
再次谢谢hopewell
作者: shiyiming    时间: 2010-8-31 00:57
标题: Re: 数据集删除观测问题
[code:3xe8mkq6]proc sql;
create table ab as
  select a.*,(a.id=b.id) as h
    from a left join b
      on a.id=b.id;
quit;[/code:3xe8mkq6]

That is my code, hope it works.

I have question for hopewell and could you explain for me.

1. ' from a left join (select distinct id, 1 as h from b) b ' . what does '1' stands for?
2. '  case b.h when 1 then 1 else 0 end as h '. I knew 'case when ...' expression,
     but never saw 'case expression' like you did here, and it is absolutly correct. So could say more about your solution.

Thanks millions and really appreciate.
作者: shiyiming    时间: 2010-8-31 15:32
标题: Re: 数据集删除观测问题
[code:zaq0g1f4]proc sort data=a;by id;run;
proc sort data=b;by id;run;

data c;
merge a(in=_a_) b(in=_b_);
by id;
if _a_ and _b_ then h=1;else h=0;
run;[/code:zaq0g1f4]

一个笨办法




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