楼主: griffingrf
4549 7

[问答] 请教一个SAS中SQL查询的问题 [推广有奖]

  • 0关注
  • 14粉丝

讲师

37%

还不是VIP/贵宾

-

威望
0
论坛币
74 个
通用积分
0.0044
学术水平
24 点
热心指数
25 点
信用等级
13 点
经验
3876 点
帖子
112
精华
0
在线时间
877 小时
注册时间
2008-4-23
最后登录
2022-6-8

楼主
griffingrf 发表于 2011-3-24 13:07:44 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
SAS菜鸟,想处理国际贸易数据现在有9张表,载有9类商品的贸易数据,每张表都有四个变量,即Reporter (出口国),Partner (进口国), Commodity Value(特定商品的贸易流量),Year (统计年份)。
Reporter是Group of Seven里面的七国,Partner是UN COMTRADE数据库里面能获取的所有国家。
9张表的区别在于Commodity Value这一项不一样,比如有的为pharmacy(药品)的贸易流量,有的为Aerospace (航空设备)的贸易流量。
现在我想9张表联合查询,弄出Reporter, Partner, Year, Pharmacy(Commodity1), Aerospace(Commodity2), Commodity3, ...... , Commodity9 的数据形式
编了一个初步的程序如下:
proc sql;
create table g7.hitech as
SELECT distinct
a.Reporter, a.Partner,a.Year, b.aerospace, c.office, a.telecom, e.pharmacy, f.sci, g.emachi, h.chem, i.nemachi, j.aa
FROM g7.telecom as a
LEFT JOIN g7.aerospace as b on a.Reporter = b.Reporter and a.Partner = b.Partner and a.Year = b.Year
LEFT JOIN g7.office as c on a.Reporter = c.Reporter and a.Partner = c.Partner and a.Year = c.Year
LEFT JOIN g7.pharmacy as e on a.Reporter = e.Reporter and a.Partner = e.Partner and a.Year = e.Year
LEFT JOIN g7.sci as f on a.Reporter = f.Reporter and a.Partner = f.Partner and a.Year = f.Year
LEFT JOIN g7.emachi as g on a.Reporter = g.Reporter and a.Partner = g.Partner and a.Year = g.Year
LEFT JOIN g7.chem as h on a.Reporter = h.Reporter and a.Partner = h.Partner and a.Year = h.Year
LEFT JOIN g7.nemachi as i on a.Reporter = i.Reporter and a.Partner = i.Partner and a.Year = i.Year
LEFT JOIN g7.aa as j on a.Reporter = j.Reporter and a.Partner = j.Partner and a.Year = j.Year
;
quit;
使用telecom这个表做LEFT JOIN的主表主要是因为其观测值最多。
不过这个程序有个问题,就是有的国家有Pharmacy或aerospace的贸易,但是没有telecom的贸易,因此这些观测值就被忽略了;若使用FULL JOIN,则这类Pharmacy或Aerospace的观测值会出现在新表中,但是这些值对应的Reporter, Partner, Year这三个指标就成了缺失值。
我希望保留这些观测值,同时也将其Reporter, Partner, Year的指标也放入新表中
求大侠帮忙解答,感激不尽。
二维码

扫码加我 拉你入群

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

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

关键词:sql un comtrade left join Aerospace Commodity 请教 SAS 查询 sql

回帖推荐

myc_sas 发表于3楼  查看完整内容

2# griffingrf 应该有两个办法: 一是对所有表proc sort排序然后merge,类似: proc sort data=a; by reporter partner year; run; /* . . . */ proc sort data=i; by reporter partner year; run; data test; merge a b c d e f g h i; by reporter partner year; run;

本帖被以下文库推荐

沙发
griffingrf 发表于 2011-3-24 15:00:01
自己顶一下哈

藤椅
myc_sas 发表于 2011-3-24 17:22:30
2# griffingrf

应该有两个办法:
一是对所有表proc sort排序然后merge,类似:

proc sort data=a;
    by reporter partner year;
run;
/*
.
.
.
*/
proc sort data=i;
    by reporter partner year;
run;

data test;
    merge a b c d e f g h i;
    by reporter partner year;
run;


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

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

板凳
myc_sas 发表于 2011-3-24 17:23:02

二是sql里用full join以及coalesce函数,类似:

proc sql;
    create table test as
        select distinct
            coalesce(a.reporter,/*...*/,i.reporter) as reporter,
            coalesce(a.partner,
/*...*/,i.partner) as partner,
            coalesce(a.year,
/*...*/,i.year) as year,
            a.telecom, b.aerospace,
/*...*/
        from (a full join b
        on a.reporter=b.reporter and /*...*/)
        full join c
        on coalesce(a.reporter,b.reporter)=c.reporter and /*...*/
        full join d
        on coalesce(a.reporter,b.reporter,c.reporter)=d.reporter and /*...*/
        /*
        .
        .
        .
        */
    ;
quit;

报纸
Actree 发表于 2011-3-24 21:02:02
难道代码的显示变了吗?我来试一下:
  1. data test;
  2.         y=1;
  3. run;
复制代码
data test;
   y=1;
run;

代码显示没变啊?楼上的怎么输进那种表格?

地板
grandhe 发表于 2011-3-25 09:32:14
5# Actree

试试以下的代码,应该是可以的了。呵呵
proc sql;
create table table as
select a.*,b.commodity1 as Pharmacy,c.commodity2 as Aerospace,d.commoditry3 as...
where a.Reporter=b.Reporter
and a.Reporter=c.Reporter
and a.Reporter=d.Reporter
and a.Reporter=e.Reporter
and a.Reporter=f.Reporter
and a.Reporter=g.Reporter
and a.Reporter=h.Reporter
and a.Reporter=i.Reporter
and a.paertner=b.partner
and a.paertner=c.partner
and a.paertner=d.partner
and a.paertner=e.partner
and a.paertner=f.partner
and a.paertner=g.partner
and a.paertner=h.partner
and a.paertner=i.partner
and a.year=b.year
and a.year=c.year
and a.year=d.year
and a.year=e.year
and a.year=f.year
and a.year=g.year
and a.year=h.year
and a.year=i.year;
run;

7
griffingrf 发表于 2011-3-25 10:19:04
3# myc_sas 哈哈~~两种方法都很有用哈~~~谢啦!

8
weberxu 发表于 2012-2-7 16:27:52
受教了

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

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