楼主: Imasasor
4511 26

如何找重复的id号 [推广有奖]

21
Imasasor 发表于 2013-11-4 14:31:15
pobel 发表于 2013-10-29 12:49
data wanted(keep=id1 id2);
   set tem;
   length _id1 $100;
quote函数不需要也可以吧,试了一下,不影响啊
欢迎加入亚太地区第一R&Python数据挖掘群: 251548215;

22
Imasasor 发表于 2013-11-4 14:32:43
可~乐 发表于 2013-10-29 14:03
这个想法真是奇特,不过,看了一大圈也没想明白为什么要scan(compress(put这么长一圈,我试了一下
if index("&id1.",cats(id2))>_n_ or index("&id1.",cats(id2))=0;

这样就可以了
欢迎加入亚太地区第一R&Python数据挖掘群: 251548215;

23
pobel 在职认证  发表于 2013-11-4 14:53:57
Imasasor 发表于 2013-11-4 14:31
quote函数不需要也可以吧,试了一下,不影响啊
版主给出的例子里,quote函数不必要。
下面的这个例子,quote函数就有用了:
data tem;
input id1 id2;
cards;
11 2
2 1
;
和谐拯救危机

24
可~乐 发表于 2013-11-4 16:55:57
Imasasor 发表于 2013-11-4 14:32
这个想法真是奇特,不过,看了一大圈也没想明白为什么要scan(compress(put这么长一圈,我试了一下
if in ...
嗯,之前做过一个类似的对比,不过需要一个字符串一个字符串的查找,所以就把之前的想法直接搬过来了。。。

25
Imasasor 发表于 2013-11-4 17:18:59
pobel 发表于 2013-11-4 14:53
版主给出的例子里,quote函数不必要。
下面的这个例子,quote函数就有用了:
data tem;
理解了,高..........
欢迎加入亚太地区第一R&Python数据挖掘群: 251548215;

26
邓贵大 发表于 2013-11-4 22:01:58
Imasasor 发表于 2013-11-4 17:18
理解了,高..........
Imasasor, what's your expected output for a 5-cycle, or it just won't happen? I know sometimes you're just handing out easy Lun Tan Bi to get more posts.
  1. data tem;
  2. input id1 id2;
  3. cards;
  4. 1 2
  5. 2 3
  6. 3 4
  7. 4 5
  8. 5 1
  9. ;

  10. proc sql noprint;
  11.         select id1 into :id1 separated by "" from tem;
  12. quit;

  13. %put &id1.;

  14. data test;
  15.         set tem;;
  16.         if index("&id1.",scan(compress(put(id2,best12.)),1))>_n_ or index("&id1.",scan(compress(put(id2,best12.)),1))=0;
  17.         keep id1 id2;
  18. run;
  19. proc print;title 'Coke';
  20. data wanted(keep=id1 id2);
  21.    set tem;
  22.    length _id1 $100;
  23.    retain _id1 ;
  24.    if index(_id1,quote(cats(id2))) then flag=0;
  25.    else do;
  26.         flag=1;
  27.         _id1=catx(",",_id1,quote(cats(id1)));
  28.         end;
  29.         if flag;
  30. run;

  31. proc print;title 'Pobel';

  32. /* the code works for the given data, not try for other data */


  33. data repeated;
  34.      set tem (rename=(id1=_id1 id2=_id2));
  35.        n = _n_;
  36.      do i = n+1 to nobs;
  37.        set tem nobs=nobs point=i;
  38.        if _id1 = id2 then output;
  39.      end;
  40.     keep id1 id2;
  41. run;

  42. proc sql;
  43.   create table want as
  44.     select *
  45.     from tem
  46.     except select * from repeated;
  47. quit;
  48. proc print data=want; title 'Yongyitian';run;


  49. data tem1(rename=(id1=id2 id2=id1));
  50.         if _n_=1 then do;
  51.                 declare hash h(dataset:"tem");
  52.                 h.definekey('id1');
  53.                 h.definedata('id2');
  54.                 h.definedone();
  55.         end;
  56.         set tem(rename=(id1=id2 id2=id1));
  57.         if h.find() NE 0 then output;
  58. run;
  59. proc print;title 'Playmore';

  60. data tem1;
  61. array id(5,3) _temporary_;
  62. infile cards eof=end;
  63. input ;
  64. id(_N_,1)=input(scan(_infile_,1), best.);
  65. id(_N_,2)=input(scan(_infile_,2), best.);
  66. return;

  67. end:

  68.   do i=1 to dim(id,1)-1;
  69.     do j=i+1 to dim(id,1);
  70.           if  id(i,1)=id(j,2) then id(j,3)=0;
  71.         end;
  72.   end;
  73.   do i=1 to dim(id,1);
  74.      if id(i,3) ne 0 then do;
  75.            id1=id(i,1);
  76.        id2=id(i,2);
  77.            output;
  78.          end;
  79.   end;
  80. keep id:;
  81. datalines;
  82. 1 2
  83. 2 3
  84. 3 4
  85. 4 5
  86. 5 1
  87. ;

  88. proc print;title 'Bobguy';run;
复制代码
已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
Imasasor + 100 + 100 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 100  论坛币 + 100  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

27
Imasasor 发表于 2013-11-4 23:04:43
邓贵大 发表于 2013-11-4 22:01
Imasasor, what's your expected output for a 5-cycle, or it just won't happen? I know sometimes you ...
事实上我要的东西没那么复杂,我原始的数据是夫妻对,一对id id1对应一对夫妻,有的时候夫妻巅倒顺序会出现两次,所以去掉一次,只保留一次。
就是如果是1,2,下面出现的一定是2,1,并且只会出现一次
欢迎加入亚太地区第一R&Python数据挖掘群: 251548215;

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-24 12:53