SAS中文论坛

标题: 急等答案,关于数据集的修改! [打印本页]

作者: shiyiming    时间: 2009-10-23 14:06
标题: 急等答案,关于数据集的修改!
数据集L中有N个变量,其中有两个变量No 和Sign,两个变量的数据类似以下
No                Sign
s1                1
s1                1
s2                1
s2                2
s3                1  
s3                1
s4                2
s4                2
现要求数据集中No相同且Sign都为1的删除,No相同但sign中有一个不为1的两个都保留
请问如何编程?谢谢
作者: shiyiming    时间: 2009-10-23 14:29
标题: Re: 急等答案,关于数据集的修改!
这跟我刚才问的问题差不多,你可以参考一下我的帖子中大牛的回帖。我帮你修改了一下那个,你试试?
[code:1m3ecbdd]
proc sort data=L out=temp;
   by no;
run;

data temp(drop=flag:);
   do _n_=1 by 1 until(last.no);
      set temp;
      by no;
      if sigh=1 then flag1=1;
      else flag2=1;
   end;
   if flag1 and flag2 then flag=1;
   do _n_=1 to _n_;
      set temp;
      by no;
      if flag=1 then output;
   end;
run;
[/code:1m3ecbdd]
作者: shiyiming    时间: 2009-10-23 14:51
标题: Re: 急等答案,关于数据集的修改!
那原来数据集中的其他变量还会保留吗?我需要把其他变量保留下来
作者: shiyiming    时间: 2009-10-23 15:00
标题: Re: 急等答案,关于数据集的修改!
会保留啊,你运行个程序看看就知道了,那里只是drop了新加的那个参数。
作者: shiyiming    时间: 2009-10-23 15:11
标题: Re: 急等答案,关于数据集的修改!
我试试,谢谢!
作者: shiyiming    时间: 2009-10-26 21:00
标题: Re: 急等答案,关于数据集的修改!
[code:39few8io]data a;
input no $ singn;
datalines;
s1 1
s1 1
s2 1
s2 2
s3 1
s3 1
s4 2
s4 2
;
run;
proc sql;
drop tabale out;
create table bc as
    select c.no,c.singn,contno,contsn
        from (select no,count(no) as contno  from a group by no) b,
             (select no,singn,count(no and singn) as contsn from a group by no,singn) c
       where b.no=c.no
       having contno>contsn;
insert into bc
    select c.no,c.singn,contno,contsn
            from (select no,count(no) as contno  from a group by no) b,
                 (select no,singn,count(no and singn) as contsn from a group by no,singn) c
            where b.no=c.no and singn^=1
            having contno=contsn;
%macro sql(no,singn);
          proc sql;
            create table out1 as
               select no,singn
               from a
               where no=%bquote('&no') and singn=&singn;
         proc append base=out data=out1;
         run;
%mend;
data _null_;
    set bc;
    call execute('%sql('||no||','||singn||')');
run;
[/code:39few8io]
作者: shiyiming    时间: 2009-10-27 12:19
标题: Re: 急等答案,关于数据集的修改!
[code:39yydmyj]data ahuige;
    input no $  sign;
    cards;
s1 1
s1 1
s2 1
s2 2
s3 1
s3 1
s4 2
s4 2
;
run;
proc sql;
    create table final as
    select *
    from ahuige
    group by no
    having max(sign) ne 1 or min(sign) ne 1
    ;[/code:39yydmyj]




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