楼主: jihezi927
13476 27

如何使用SAS中某一表的数据更新另一表中的数据 [推广有奖]

11
jihezi927 发表于 2013-5-17 01:34:29
YobachiLiu 发表于 2013-5-16 20:47
update 产品表 set 价格=价格*1.03
where id in(select id from 销售的产品表);
如果这是个重复性的或者有 ...
恩,那如果我已经有一个table是有所有新的价格,我想关联进那个所有产品的表,并不是使用=price* 1.03 这个公式,那应该怎么写呢?
千江有水千江月

12
YobachiLiu 发表于 2013-5-17 09:51:57
jihezi927 发表于 2013-5-17 01:34
恩,那如果我已经有一个table是有所有新的价格,我想关联进那个所有产品的表,并不是使用=price* 1.03 这 ...
update 销售产品表 a  

  set    价格=(select b.价格  from 价格表 b where b.产品id=a.产品id)

  where  exists (select 1

  from   价格表 b

  where  b.产品_id=a.产品id
  )

13
milos_luna 发表于 2013-5-17 10:13:24
我这个效率就低了,哈哈,上面2位的方法好

Proc SQL;
                        create table three as
                        select a.id, case  when a.id = b.id then b.price
                                                 when a.id ^= b.id then a.price
                                                 end as price
                                                 from one as a, two as b;
                        Quit;

14
yongyitian 发表于 2013-5-17 11:10:42
/* in sql, use coalesces() function with left join */
/* in data step merge, sort both data set by id, */
/* and have same name for price variables */

data Prd_price;
   input Id $4.  price;
datalines;
0001 110
0002 120
0003 130
0004 140
; run;

data sale;
   input Id $4. price;
datalines;
0001 111
0003 133
; run;

proc sql noprint;
   create table result as
   select a.id, coalesce(b.new_price, a.price) as price
   from prd_price a left join
                    (select id, price*1.03 as new_price
                       from sale ) as b
     on a.id=b.id;
quit;

/* use merge */
data sale_new;
    set sale;
    price = price*1.03;
run;

proc sort data=prd_price; by id; run;
proc sort data=sale_new;  by id; run;

data new_price;
    merge prd_price sale_new;
    by Id;
run;

15
jinsen78 发表于 2013-5-17 20:21:38
data product_price;
input id p;
cards;
001        46
002        44
003        6
004        79
005        73
006        60
007        70
008        31
009        10
010        93
;
run;
data product_sell;
input id p;
cards;
001        47.46
002        45.10
003        5.80
004        81.61
005        74.97
006        61.33
007        72.33
008        31
009        10
010        93
;
run;
data product_price;/*《sas编程与数据挖掘商业案例》p42,有详细的解释*/
modify product_price product_sell;
by id;
run;
这个应该能满足要求。。

16
jihezi927 发表于 2013-5-21 00:36:39
邓贵大 发表于 2013-5-17 03:03
Same idea @YobachiLiu, use subquery. And my previous comment was flawed, 'LEFT JOIN' works fine if u ...
proc sql;
        update mylib.pp1 p
                set sale_price = (
                        select sales_price from mylib.bs1 b
                                where p.sku=b.sku
                )
                where sku in (
                        select sku from mylib.bs1
                );
quit;
但是显示有错误,
ERROR: Subquery evaluated to more than one row.
千江有水千江月

17
jihezi927 发表于 2013-5-21 00:41:35
邓贵大 发表于 2013-5-17 03:03
Same idea @YobachiLiu, use subquery. And my previous comment was flawed, 'LEFT JOIN' works fine if u ...
coalesce(b.totpaidclaims, a.paidclaims) as paidclaims 成功了!!

非常感谢,非常感谢。

千江有水千江月

18
jihezi927 发表于 2013-5-21 00:48:58
YobachiLiu 发表于 2013-5-17 09:51
update 销售产品表 a  

  set    价格=(select b.价格  from 价格表 b where b.产品id=a.产品id)
不好意思,那个1代表什么意思啊。

我试了一下,好像还是不行
千江有水千江月

19
jihezi927 发表于 2013-5-21 00:51:42
YobachiLiu 发表于 2013-5-17 09:51
update 销售产品表 a  

  set    价格=(select b.价格  from 价格表 b where b.产品id=a.产品id)
proc sql;
update mylib.pp1 a
set sale_price=(select b.sales_price from mylib.bs1 b where b.sku=a.sku)
     where exists ( select b.sku from mylib.bs1 b where b.sku=a.sku);
quit;
错误信息是:
ERROR: Subquery evaluated to more than one row
千江有水千江月

20
jihezi927 发表于 2013-5-21 00:53:06
邓贵大 发表于 2013-5-16 13:40
Same idea @YobachiLiu, use subquery. And my previous comment was flawed, 'LEFT JOIN' works fine if u ...
另外,如果用left join, 有没有什么办法是不用create一个新table去更新价格呢?
千江有水千江月

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-9 05:32