楼主: yz7891
2478 12

来人看看 SAS [推广有奖]

  • 0关注
  • 0粉丝

本科生

86%

还不是VIP/贵宾

-

威望
0
论坛币
18 个
通用积分
0
学术水平
0 点
热心指数
4 点
信用等级
0 点
经验
615 点
帖子
100
精华
0
在线时间
61 小时
注册时间
2010-3-30
最后登录
2019-6-3

楼主
yz7891 发表于 2010-4-1 08:53:21 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
表Store:
Store_id(pk)    City                Space
wh1                New York        370
wh2                Beijing            500
wh3                New York        200
wh4                London           400

表Employee:
Store_id(fk)     Emp_ID(pk)       Salary
wh1                e1                     1220
wh1                e3                     1210
wh2                e4                     1250
wh2                e6                     1230


需要得到结果:每个城市的 总Salary,总Space,在Store表中数量,在Employee表中数量,最大Salary 得出结果如下:
City             Tot.Salary                  Tot.Space            #Store     #Employee      Max Salary
New York     2430(1220+1210)       570(370+200)         2                2                   1220
Beijing         2480(1250+1230)       500                        1                2                   1250
London        .                                400                        1                0                   .



  1. data one;
  2. input Store_id $ City $ Space;
  3. cards;
  4. wh1 New.York 370
  5. wh2 Beijing 500
  6. wh3 New.York 200
  7. wh4 London 400
  8. ;
  9. data two;
  10. input Store_id $ Emp_ID $ Salary;
  11. cards;
  12. wh1 e1 1220
  13. wh1 e3 1210
  14. wh2 e4 1250
  15. wh2 e6 1230
  16. ;
  17. proc sort data=one;
  18. by Store_id;
  19. proc sort data=two;
  20. by Store_id;
  21. data total;
  22. merge one two;
  23. by Store_id;
  24. data total1;
  25. set total;

  26. proc sql;
  27. select City,sum(salary) as totsalary ,count(Emp_id) as Employee,max(salary)  from total1  group by City ;
  28. run;
复制代码


我现在只能查询出
                                                                          The SAS System            

                                                               City      totsalary  Employee
                                                               儍儍儍儍儍儍儍儍儍儍儍儍儍儍儍儍儍儍儍?
                                                               Beijing        2480         2      1250
                                                               London            .           0         .
                                                               New.York     2430       2      1220

如果加上其他的选项的话就会狂出问题,谁能告诉我应该怎么做这个???
谢谢
二维码

扫码加我 拉你入群

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

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

关键词:Employee proc sql Beijing salary LONDON SAS 来人

回帖推荐

醉_清风 发表于9楼  查看完整内容

data one; input Store_id $ City $ Space; n+1; cards; wh1 New.York 370 wh2 Beijing 500 wh3 New.York 200 wh4 London 400 ; run; data two; input Store_id $ Emp_ID $ Salary; cards; wh1 e1 1220 wh1 e3 1210 wh2 e4 1250 wh2 e6 1230 ; run; proc sql; create table three as select A.City ,sum(B.t_Salary) as Tot_Salary ,sum(A.Space) as Tot_Space ,count ...

本帖被以下文库推荐

沙发
anzhiliang 发表于 2010-4-1 09:15:21
这样写应该没有问题的。
data one;
input Store_id $ City $ Space;
cards;
wh1 New.York 370
wh2 Beijing 500
wh3 New.York 200
wh4 London 400
;
data two;
input Store_id $ Emp_ID $ Salary;
cards;
wh1 e1 1220
wh1 e3 1210
wh2 e4 1250
wh2 e6 1230
;
proc sort data=one;
by Store_id;
proc sort data=two;
by Store_id;
data total;
merge one two;
by Store_id;
data total1;
set total;
run;
proc sql;
select City,sum(salary) as totsalary ,count(Emp_id) as Store,count(emp_id) as Employee,max(salary) as Maxsalary  from total1  group by City ;
quit;

藤椅
anzhiliang 发表于 2010-4-1 09:15:51
[img][/img]

板凳
anzhiliang 发表于 2010-4-1 09:17:42
运行的结果如下:
                                                      The SAS System             2010年04月01日 星期四 上午09时02分34秒   4

                                    City      totsalary     Store  Employee  Maxsalary
                                    --------------------------------------------------
                                    Beijing        2480         2         2       1250
                                    London            .         0         0          .
                                    New.York       2430         2         2       1220

报纸
anzhiliang 发表于 2010-4-1 09:18:31
当然可以用连接来做,应该就简单一些了吧。

地板
yz7891 发表于 2010-4-1 09:22:24
我试过您的代码了,前面几个都可以哦,但是我还要加上STORE表CITY项的数量,我是想通过Store_id来实现的,但是把2个表合并后,count(Store_id)就会出错的,这个应该怎么解决呢……
比如
在Store表里面的New.York 数量是2 Beijing应该是1 London是1
也就是
Store
2
1
1
这样的~~

7
soporaeternus 发表于 2010-4-1 09:33:14
以我的理解
two表是最细粒度的表,Emp-->Store-->City是维度
Salary是Emp上的度量,Space是Stroe上的

SQL查询就可以了,不一定强求data步啊
Let them be hard, but never unjust

8
yz7891 发表于 2010-4-1 09:41:15
soporaeternus 发表于 2010-4-1 09:33
以我的理解
two表是最细粒度的表,Emp-->Store-->City是维度
Salary是Emp上的度量,Space是Stroe上的

SQL查询就可以了,不一定强求data步啊
这个我在数据库中用SQL语句可以查询出来,但是问我问题这人要在SAS中实现 我以前也没学过SAS 只能照着PDF文档里面的东西来描,但是一直出错,麻烦能告诉下怎么做么?或者能不能贴个代码出来 谢谢

9
醉_清风 发表于 2010-4-1 09:55:15
data one;
input Store_id $ City $ Space;
n+1;
cards;
wh1 New.York 370
wh2 Beijing 500
wh3 New.York 200
wh4 London 400
;
run;

data two;
input Store_id $ Emp_ID $ Salary;
cards;
wh1 e1 1220
wh1 e3 1210
wh2 e4 1250
wh2 e6 1230
;
run;

proc sql;
        create table three as
        select   A.City
                        ,sum(B.t_Salary) as Tot_Salary                  
                        ,sum(A.Space) as Tot_Space
                        ,count(A.City) as store
                        ,sum(B.Emp_num) as Employee
                        ,max(B.m_Salary) as Max_Salary
        from one A
        left join (select Store_id ,count(Emp_ID) as emp_num ,sum(Salary) as t_Salary,max(Salary) as m_Salary
                           from two group by Store_id) B
        on A.Store_id=B.Store_id
        group by City;
quit;
跑了一下 没仔细看 希望对你有帮助
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

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

从来不需要想起 永远也不会忘记

10
soporaeternus 发表于 2010-4-1 09:56:56
  1. proc sql;
  2.         create table three as
  3.                 select
  4.                         a.City
  5.                         ,b.Tot_Salary
  6.                         ,a.Tot_Space
  7.                         ,a.Store_Cnt
  8.                         ,b.Employee_Cnt
  9.                         ,b.Max_Salary
  10.                 from
  11.                         (select
  12.                         City
  13.                         ,sum(Space) as Tot_Space
  14.                         ,count(Store_Id) as Store_Cnt
  15.                         from one
  16.                         group by City
  17.                         ) a
  18.                 left join
  19.                  (select
  20.                         b.City
  21.                         ,sum(a.Salary) as Tot_Salary
  22.                         ,count(a.Emp_Id) as Employee_Cnt
  23.                         ,max(a.Salary) as Max_Salary
  24.                  from two a inner join one b
  25.                  on        a.Store_Id=b.Store_Id
  26.                  group by b.City
  27.                         ) b
  28.                 on a.City=b.City
  29.                 ;
  30. quit;
复制代码
不知道全量数据会不会有Store没有Emp,甚至有City没有Store
用个left join保险点
Let them be hard, but never unjust

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

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