楼主: dengwei715
1722 4

[问答] 求助,怎么用merge实现下面sql语句的功能 [推广有奖]

  • 0关注
  • 1粉丝

本科生

78%

还不是VIP/贵宾

-

威望
0
论坛币
29 个
通用积分
0.0004
学术水平
4 点
热心指数
7 点
信用等级
1 点
经验
930 点
帖子
148
精华
0
在线时间
63 小时
注册时间
2008-9-5
最后登录
2018-3-7

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
data test;
input x y;
cards;
1 2
1 3
1 4
2 3
2 4
2 8
3 4
;
run;

proc sql; create table test_a as select
a.x as x_a,  b.x as x_b, a.y
from test as a inner join test as b
on a.y=b.y and a.x<b.x;
quit;

二维码

扫码加我 拉你入群

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

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

关键词:sql语句 Merge sql inner join proc sql sql语句

本帖被以下文库推荐

沙发
yongyitian 发表于 2014-2-18 12:46:12 |只看作者 |坛友微信交流群
  1. data test;
  2. input x y;
  3. cards;
  4. 1 2
  5. 1 3
  6. 1 4
  7. 2 3
  8. 2 4
  9. 2 8
  10. 3 4
  11. ;
  12. run;

  13. data test_a;
  14.     set test(rename=(x=x_a y=y_a));
  15.     id = _n_;
  16. run;

  17. data test_b;
  18.    do Id = 1 to nobs;
  19.      do p = 1 to nobs;
  20.      set test(rename=(x=x_b y=y_b))  nobs=nobs point=p;
  21.          output;
  22.      end;
  23.    end; stop;
  24. run;

  25. data test_wanted;
  26.     merge test_a test_b;
  27.         by id;
  28.         if x_a < x_b and y_a = y_b;
  29.         drop id y_a;
  30.         rename y_b = y;
  31. run;
  32. proc print data=test_wanted; title 'test_wanted'; run; title;
复制代码
Test_wanted.JPG
已有 1 人评分经验 学术水平 收起 理由
李会超 + 40 + 2 分析的有道理

总评分: 经验 + 40  学术水平 + 2   查看全部评分

使用道具

藤椅
yanjiyunning 发表于 2014-12-4 15:13:01 |只看作者 |坛友微信交流群
data test;
input x y;
cards;
1 2
1 3
1 4
2 3
2 4
2 8
3 4
;
run;

data a;
set test;
rename x=x_a;
run;
data b;
set test;
rename x=x_b;
run;
proc sort data =a ;
by y;
run;
proc sort data =b;
by y;
run;
data test_a ;
merge a (in =a)  b (in =b);
by y ;
if (a =1);
if   x_a < x_b;
run;

使用道具

板凳
Tigflanker 发表于 2014-12-5 23:01:42 |只看作者 |坛友微信交流群
  1. data test_b;
  2.   drop x;
  3.   if 0 then set test;
  4.   if _n_ = 1 then do;
  5.     dcl hash h(dataset:'test',multidata:'y');
  6.         h.definekey('y');
  7.         h.definedata(all:'y');
  8.         h.definedone();
  9.   end;

  10.   set test;
  11.     x_a = x;
  12.   if ~h.find() then do;
  13.     if x_a < x then do;x_b = x;output;end;

  14.     do while(~h.find_next() and x_a < x);
  15.       x_b = x;output;
  16.         end;
  17.   end;
  18. run;
复制代码

使用道具

报纸
Eternal0601 发表于 2014-12-6 22:49:49 |只看作者 |坛友微信交流群
Tigflanker 发表于 2014-12-5 23:01
貌似少了一条记录,改了下,哈哈
data final(drop=x);
        if 0 then set test;
        if _n_ = 1 then do;
                        dcl hash h(dataset:'test',multidata:'y');
                        h.definekey('y');
                        h.definedata(all:'y');
                        h.definedone();
        end;

        set test;

        x_a = x;

        if ~h.find() then do;
                if x_a < x then do;
                                                        x_b = x;
                                                        output;
                                                end;

                do while(~h.find_next() );
                        x_b = x;
                        if x_a < x_b then output;
                end;
        end;
run;
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
Tigflanker + 5 + 3 + 3 + 3 喔?谢谢哈^_^

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

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-26 10:23