我想用proc compare
每天更新数据集。
work.HAVE1
Date Key Var1 Var2
01Aug2013 K1 a 2
01Aug2013 K2 a 3
02Aug2013 K1 b 4
work.HAVE2
Date Key Var1 Var2
01Aug2013 K1 a 3
01Aug2013 K2 a 3
02Aug2013 K1 b 4
03Aug2013 K2 c 1
Date
and Key
唯一确定一条记录。
我如何使用上面两个表来构建以下
work.WANT
Date Key Var1 Var2
01Aug2013 K1 a 3
01Aug2013 K2 a 3
02Aug2013 K1 b 4
03Aug2013 K2 c 1
我不想删除以前的数据然后重建它。我想要modify
它通过在底部附加新记录并调整中的值VAR1
or VAR2
。
我正在挣扎proc compare
但它只是没有返回我想要的。
proc compare base=work.HAVE1 compare=work.HAVE2 out=WORK.DIFF outnoequal outcomp;
id Date Key;
run;
这将为您提供单个数据集 WORK.DIFF 中的新记录和更改记录(不相等的记录)。你必须自己区分新的和改变的。
然而,你想要实现的实际上是MERGE
- 插入新的,覆盖现有的,尽管可能是由于性能原因等,您不想重新创建完整的表。
data work.WANT;
merge work.HAVE1 work.HAVE2;
by Date Key;
run;
Edit1:
/* outdiff option will produce records with _type_ = 'DIF' for matched keys */
proc compare base=work.HAVE1 compare=work.HAVE2 out=WORK.RESULT outnoequal outcomp outdiff;
id Date Key;
run;
data WORK.DIFF_KEYS; /* keys of changed records */
set WORK.RESULT;
where _type_ = 'DIF';
keep Date Key;
run;
/* split NEW and CHANGED */
data
WORK.NEW
WORK.CHANGED
;
merge
WORK.RESULT (where=( _type_ ne 'DIF'));
WORK.DIFF_KEYS (in = d)
;
by Date Key;
if d then output WORK.CHANGED;
else output WORK.NEW;
run;
Edit2:
现在你可以APPEND
WORK.NEW 到目标表。
对于 WORK.CHANGED - 要么使用MODIFY
or UPDATE
声明更新记录。
根据变化的大小,你还可以考虑PROC SQL; DELETE
删除旧记录并PROC APPEND
添加新值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)