我有2个数据集如下
id name status
1 A a
2 B b
3 C c
另一个数据集
name status new
C c 0
D d 1
E e 1
F f 1
如何将第二个表中的所有行插入到第一个表中?情况是第一个表是永久性的。第二张表每月更新一次,所以我想将每月更新表中的所有行添加到永久表中,这样它看起来像这样
id name status
1 A a
2 B b
3 C c
4 D d
5 E e
6 F f
我面临的问题是我无法从数据集 1 中增加 id。据我搜索,SAS 中的数据集没有自动增量属性。自动增量可以通过使用数据步骤来完成,但我不知道数据步骤是否可以用于像这样的 2 个表的情况。通常的sql是
Insert into table1 (name, status)
select name, status from table2 where new = 1;
但由于 sas 数据集不支持自动增量列,因此我面临的问题。我可以在上面的proc sql之后使用SAS数据步骤来解决它
data table1;
set table1;
if _n_ > 3 then id = _n_;
run;
这会增加 id 列的值,但代码有点丑,而且 id 是主键,在其他表中用作外键,所以我不想弄乱旧行的 id .
我正在学习和使用 SAS,因此非常感谢您的帮助。提前致谢。
额外问题:如果第二张表没有新列,有没有办法用数据步骤完成我想要的(从月度表(2nd)到永久表(1st)添加新行)?目前,我使用这个丑陋的 proc sql/data 步骤来创建新列
proc sql; //create a temp table from table2
create t2temp as select t2.*,
(case when t2.name = t1.name and t2.status = t1.status then 0 else 1) as new
from table2 as t2
left join table1 as t1
on t2.name = t1.name and t2.status = t1.status;
drop table t2; //drop the old table2 with no column "new"
quit;
data table2; //rename the t2temp as table2
set t2temp;
run;