我正在寻找一个与 Excel 中的“VLOOKUP”函数一样工作的 SAS 代码。
我有两张 table :
table_1 有一个 ID 列,其中包含 10 行的其他一些列。 Table_2 有两列:ID 和定义,共 50 行。我想在 table_1 中定义一个新变量“Definition”并从 table_2 中查找 ID 值。
除了合并之外,我还没有真正尝试过任何其他方法。但是合并保留了 table_2 中所有额外的 40 个变量,这不是我喜欢的。
谢谢, SE
最佳答案
最简单的方法是使用 keep
您的 merge
上的选项陈述。
data result;
merge table_1 (in=a) table_2 (in=b keep=id definition);
by id;
if a;
run;
另一种意味着您不必对数据集进行排序的方法是使用 proc sql。
proc sql;
create table result as
select a.*,
b.definition
from table_1 a
left join table_2 b on a.id = b.id;
quit;
最后,如果 table_2 很小,还有哈希表选项:
data result;
if _n_ = 1 then do;
declare hash b(dataset:'table_2');
b.definekey('id');
b.definedata('definition');
b.definedone();
call missing(definition);
end;
set table_1;
b.find();
run;
关于excel - 像 Excel 的 "VLOOKUP"函数一样工作的 SAS 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17326182/