楼主: susanyue
3492 18

[求助]如何找出是哪些变量的值不同? [推广有奖]

  • 0关注
  • 0粉丝

硕士生

79%

还不是VIP/贵宾

-

威望
0
论坛币
3004 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
21553 点
帖子
95
精华
0
在线时间
292 小时
注册时间
2008-12-25
最后登录
2015-8-28

楼主
susanyue 发表于 2009-2-20 14:38:00 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

合并几个数据集后,发现同样的id因为有些变量的值不同而有多条记录,

例如:

data a;

 input id x1 x2 @@;

cards;

1 11 12

2 21 22

4 41 42

4 411 422

5 51 52

;

data b; input id x3 @@;

cards;

1 13 3 33 4 43 5 53 5 533 5 5333

;

run;

data c;

merge a b;

by id;

proc print;

run;

得到结果如下:

obs   id     x1      x2      x3

1       1     11     12      13

2       2     21     22        .

3       3      .         .      33

4       4     41     42     43

5       4    411    422    43

6       5     51      52     53

7       5     51      52     533

8       5     51      52     5333

怎样用SAS程序把上面红色部分的找出来,并且按照id号记录,因为数据库中变量有800个,也就是说要相同id号的记录,用proc compare进行每个变量比较的话,要比较800次,想问有没有高手可以写一个程序帮忙把同一记录号变量值不同的变量找出来呢?

[此贴子已经被作者于2009-2-20 18:15:42编辑过]

二维码

扫码加我 拉你入群

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

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

关键词:Compare Input cards sas程序 print 求助 变量

回帖推荐

pobel 发表于9楼  查看完整内容

data a; input id x1 x2 @@;cards;1 11 122 21 224 41 424 411 4225 51 52;data b; input id x3 @@;cards;1 13 3 33 4 43 5 53 5 533 5 5333;run;data c;merge a b;by id;run;proc sort data=c;     by id;run;data d;     set c;  by id;  if first.id and last.id then delete;run;proc transpose data=d out=e(rename=(_name_=var));     by id;ru ...

zhitler 发表于2楼  查看完整内容

proc sql;   select id, x1, x2, x3     from c     group by id     having (max(x1) ne min(x1))            or (max(x2) ne min(x2))            or (max(x3) ne min(x3));quit;        &nbs ...

本帖被以下文库推荐

沙发
zhitler 发表于 2009-2-20 15:53:00

very simple


proc sql;

   select id, x1, x2, x3
     from c
     group by id
     having (max(x1) ne min(x1))
            or (max(x2) ne min(x2))
            or (max(x3) ne min(x3));
quit;

                            id        x1        x2        x3
                      --------------------------------------
                             4       411       422        43
                             4        41        42        43
                             5        51        52       533
                             5        51        52        53
                             5        51        52      5333

已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

藤椅
sushe1527 发表于 2009-2-20 15:56:00

晕 出去拿了个快递来晚了

data result(drop=i);
   set c;
   by id notsorted;
   if first.id then i=0;
   i+1;
   if last.id and i=1 then delete;
   if x1=. or x2=. or x3=. then delete;
run;

[此贴子已经被作者于2009-2-20 16:17:21编辑过]

板凳
zhitler 发表于 2009-2-20 16:07:00

sushe1527,你的解法好像有点问题哟

你把a里面的数据完全重复一条,如

2 21 22

2 21 22

呵呵

[此贴子已经被作者于2009-2-20 16:07:49编辑过]

报纸
sushe1527 发表于 2009-2-20 16:14:00
以下是引用zhitler在2009-2-20 16:07:00的发言:

sushe1527,你的解法好像有点问题哟

你把a里面的数据完全重复一条,如

2 21 22

2 21 22

呵呵


没有问题啊,楼主的意思是 就是说要相同id号的记录

那么只要剔除单独的ID号码就可以了 其他的 哼哼 不管了

如果缺失的数据不能要的话 加上  if x1=. or x2=. or x3=. then delete;

[此贴子已经被作者于2009-2-20 16:20:50编辑过]

地板
zhitler 发表于 2009-2-20 16:18:00
以下是引用sushe1527在2009-2-20 16:14:00的发言:
以下是引用zhitler在2009-2-20 16:07:00的发言:

sushe1527,你的解法好像有点问题哟

你把a里面的数据完全重复一条,如

2 21 22

2 21 22

呵呵


没有问题啊,楼主的意思是 就是说要相同id号的记录

那么只要剔除单独的ID号码就可以了 其他的 哼哼 不管了

可能是我想多了吧[em01]

7
susanyue 发表于 2009-2-20 18:06:00

不好意思,请看一下最后的要求,那只是个例子,我的数据库有800个变量,而且变量名有长有短,不可能全部列不出来,否则就太麻烦了,还有我只要把红色的部分弄出来,不要整条记录!

8
susanyue 发表于 2009-2-20 18:12:00

data result(drop=i);
   set c;
   by id notsorted;
   if first.id then i=0;
   i+1;
   if last.id and i=1 then delete;
   if x1=. or x2=. or x3=. then delete;
run;

这个程序其实我已经做了,现在其实我手上就是只有多条记录的库,但实际上我只是想在这个库中把变异找出,其实不要整条记录,因为变量有800个,所以我想可能会用到循环,可是我不知道怎么写这个程序,就是以记录号id为基础,对每个记录号的所有记录的所有变量进行一一比较,从中把不一样的找出来。不知道可行不可行?

9
pobel 在职认证  发表于 2009-2-21 08:41:00

data a;
 input id x1 x2 @@;
cards;
1 11 12
2 21 22
4 41 42
4 411 422
5 51 52
;

data b; input id x3 @@;
cards;
1 13 3 33 4 43 5 53 5 533 5 5333
;
run;

data c;
merge a b;
by id;
run;

proc sort data=c;
     by id;
run;

data d;
     set c;
  by id;
  if first.id and last.id then delete;
run;

proc transpose data=d out=e(rename=(_name_=var));
     by id;
run;

data d1;
     set d;
  by id;
  if first.id then n=1;
  else n+1;
  if last.id;
  keep id n;
run;

data temp;
     merge e d1;
  by id;
run;

options mprint symbolgen mlogic;
%macro compare;
data f;
     merge e d1;
  by id;
run;

data g;
     set f end=last;
  call symput('num'||left(_n_),trim(left(n)));
  if last then call symput("tot",left(_n_));
run;
data h;
     set g;
  %do t=1 %to &tot;
      if _n_=&t then do;
                       %do i=1 %to &&num&t;
                              %do j=%eval(&i+1) %to &&num&t;
                               if col&i ne col&j then output;
                            %end;
                           %end;

                  end;
      %end;
    
run;

proc sort data=h nodupkey;
     by id var;
run;
%mend;

%compare

已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

和谐拯救危机

10
sushe1527 发表于 2009-2-21 09:51:00

pobel 你太强大了

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

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