全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
4204 25
2014-08-19
现有数据如下, 三个column (title,authors(不同的name用|隔开),number_authors)

Title                      Authors                                                    Number_authors
Title 1               Name A | Name B                                                 2
Title 2               Name A | Name B  | Name C                                  3
Title 3               Name A | Name C  | Name E | Name Z                     4
TITLE 4                NAME A                                                           1
TITLE 5                   NAME F | NAME Z                                            2
..
大概有20000个observations,其中
1. title是unique的
2. number_authors 取值从1-200.


现在想做的是,对每一个observation生成一系列variables(5个):at_least_x_authors_repeat. X从1-5取整数值. 变量取值0或1
也就是:at_least_1_authors_repeat; at_least_2_authors_repeat;at_least_3_authors_repeat;at_least_4_authors_repeat;
at_least_5_authors_repeat.
变量描述了在这组数据中有多少作者是重复的


变量举例描述: 比如at_least_2_authors_repeat:title3有name A, name C, name E, name Z 四个author 如果其中至少两个名字在别的observation里也同时出现过,那么at_least_2_authors_repeat = 1, 如果任意两个名字在其他observation里都没有同时出现过,那么at_least_2_authors_repeat = 0.
从目前数据看来,A 和 C 在title 2 中出现过,所以title 2 和title3 的at_least_2_authors_repeat取值为1.
同样的,对于at_least_3_authors_repeat,我们需要检验至少三个.

这个程序应该怎样实现的?小弟已经冥思苦想很久了,仍然没有头绪,希望各位指点一二,谢谢!


二维码

扫码加我 拉你入群

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

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

全部回复
2014-8-19 13:35:24
比较复杂
二维码

扫码加我 拉你入群

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

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

2014-8-19 13:42:23
感觉要用hash表,hash表好实现些。
二维码

扫码加我 拉你入群

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

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

2014-8-19 13:43:58
有偿编程,价格优惠哈
二维码

扫码加我 拉你入群

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

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

2014-8-19 14:25:10
INPUT那句里的两个informat不能正常显示。

data test;
  input Title &   $10.           Authors      $60.              Number_authors ;
  authors=upcase(authors);
  cards;
Title 1    Name A | Name B                                                                                     2
Title 2    Name A | Name B  | Name C                                                                    3
Title 3    Name A | Name F  | Name B | Name Z                                                     4
Title 4    NAME A                                                                                                   1
Title 5    NAME F | NAME T                                                                                    2
Title 6    NAME F | NAME Z  | Name A | Name N | Name B | Name X                       6   
;

*** Max number of authors;
proc sql noprint;
   select distinct max(number_authors) into: maxn
     from test;
quit;
%let maxn=&maxn;
%put *&maxn*;

*** Get 1-5 authors for each title;
data test1;
    array author(&maxn) $10;
    set test;
         
        comb=2**number_authors-1;
        fmt="Binary"||cats(number_authors)||".";

    do i=1 to comb;
            k=0;
            binary=reverse(putn(i,fmt));
        call missing(of author1- author&maxn);
                do j=1 to number_authors;
                    if substr(binary,j,1)="1" then do;
                            k+1;
                                author(k)=left(scan(authors,j,"|"));
                        end;
                end;
                call sortc(of author&maxn-author1);
                output;
        end;
        keep author1-author5 title;
run;

*** AT_LEAST_x_AUTHORS_REPEAT;
proc sql;
   create table test2 as
    select distinct author1,author2, author3,author4,author5,title,count(distinct title) as titlenum
          from test1
           group by author1,author2,author3,author4,author5
       order by author1,author2,author3,author4,author5;

quit;

data test3;
    set test2;
        by author1-author5;
        array repeat_(5);

        tmp=catx("*", of author1-author5);
        varn=count(tmp,"*")+1;
    repeat_(varn)=(titlenum>1);
run;

proc sql;
   create table author_repeat as
    select distinct title, max(repeat_1) as at_least_1_authors_repeat
                             , max(repeat_2) as at_least_2_authors_repeat
                                                 , max(repeat_3) as at_least_3_authors_repeat
                                                 , max(repeat_4) as at_least_4_authors_repeat
                                                 , max(repeat_5) as at_least_5_authors_repeat
                from test3
       group by title;
quit;


*** Merge;
data wanted;
    merge test author_repeat;
        by title;
run;
二维码

扫码加我 拉你入群

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

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

2014-8-19 15:31:39
只会写简单的程序,不知道是不是想要的结果。。。。
复制代码
二维码

扫码加我 拉你入群

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

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

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

说点什么

分享

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