楼主: jihezi927
13478 27

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

21
邓贵大 发表于 2013-5-21 01:31:27
jihezi927 发表于 2013-5-21 00:36
proc sql;
        update mylib.pp1 p
                set sale_price = (
Not a good sign. The error message indicates there exist multiple records for some SKUs in table mylib.bs1.
Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

22
邓贵大 发表于 2013-5-21 01:33:40
jihezi927 发表于 2013-5-21 00:53
另外,如果用left join, 有没有什么办法是不用create一个新table去更新价格呢?
I would say NO for SAS datasets. It'll be a different story for DBMS tables.
I'm not 100% sure though.
Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

23
jihezi927 发表于 2013-5-24 00:39:37
yongyitian 发表于 2013-5-17 11:10
/* in sql, use coalesces() function with left join */
/* in data step merge, sort both data set by  ...
以上是方法都是要create一个新的table,有什么办法只更新旧table中的数据
千江有水千江月

24
jihezi927 发表于 2013-5-24 01:09:09
yongyitian 发表于 2013-5-17 11:10
/* in sql, use coalesces() function with left join */
/* in data step merge, sort both data set by  ...
实在是不好意思哇,我才看到你的回复,应该把你的回复选为最佳答案的。

千江有水千江月

25
yongyitian 发表于 2013-5-24 10:00:58
jihezi927 发表于 2013-5-24 00:39
以上是方法都是要create一个新的table,有什么办法只更新旧table中的数据
LZ的想法是对的。 当原来的表很大时,再 create 一个新表显然是不合适的, 尤其是是当原来的表连接着其他表时。 正确的方法是用一个小的新表里的数据来更新 (update)大表中的数据。

sas 有两种更新表格的方法, 楼上已经提到了.

一种方法是用 proc sql; update table statement (沙发, 7楼, 12楼)。

proc sql;
  update oldtable as old
  set column_to_be_updated=(select column_to_be_updated
                            from newtable as new
                            where  condition ) /* such as:  where old.var1 = new.ver2) */
      where other_condition;
quit;

另一种方法是用 data step the modify statement(15楼).

另外还可一用 data step 的 update 语句。 如果用14 楼排过续的数据集。code 写为
data prd_price;
    update prd_price sale_new;
    by id;
run;

这里说的更新(update), 是指只改变原表中的数据, 而不改变原表的结构与变量的特性。

而其他的方法, 如 data step merge, proc sql; left/right join 虽然也可以用一个表的数据来替换另一个表中的数据。 但这些方法的主要目的是从多个表中找出相匹配的数据,从而形成新表或者报告。

而且新表的结构会与原表不同, 比如,加入新的变量。

如果你的数据不多, 而且与其他表格没有关系。 当然用merge 和proc sql; left/right join 方便些. 至少可以保留一份原始数据.

update 在数据库中用的比较多.

26
肝贝 发表于 2014-8-12 18:21:44
邓贵大 发表于 2013-5-16 13:40
Same idea @YobachiLiu, use subquery. And my previous comment was flawed, 'LEFT JOIN' works fine if u ...
您好 我想请教您coalesce函数在这里的应用,我百度了一些,都没能够很好地理解这个函数,拜托大神帮帮忙~~谢谢~~~~

27
肝贝 发表于 2014-8-12 18:26:58
邓贵大 发表于 2013-5-16 13:40
Same idea @YobachiLiu, use subquery. And my previous comment was flawed, 'LEFT JOIN' works fine if u ...
您好,想问您一下coalesce这个函数在这里的释义,我百度了一下,都没能很好地理解这个函数,拜托您帮帮忙,谢谢~~

28
G狼顾之鬼 发表于 2016-3-17 17:59:03
HASH方法一个data步就可以了:
***所有商品id和价格***;
data all;
        input id price;
        cards ;
1 10
2 20
3 30
4 40
5 50
6 60
;
run ;
***当天销售商品id及价格***;
data today;
        input id price;
        cards ;
1 10
2 20
3 30
;
run ;
***实现过程***;
data result;
        drop rc;
        if 0 then set today;
        if not missing(price) then price=price*1.03; ***当天售出商品  价格提高3%***;
        ***HASH方法***;
        if _N_=1 then do;
                declare hash hh(dataset:'updat');
                hh.definekey('id');
                hh.definedata('price');
                hh.definedone();
                call missing(id, price);
        end;

        set all;
        rc=hh.find();
        if rc=0 then output;  ***输出当天售出商品(提价)***;
        else output; ***输出其他商品(原价)***;
run;

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

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