与Excel的“VLOOKUP”function类似的SAS代码

我正在寻找一个像Excel中的“VLOOKUP”函数一样工作的SAS代码。

我有两个表:table_1有一个ID列,其中有一些其他列10行。 表2有两列:ID和50行的定义。 我想在table_1中定义一个新的variables“Definition”,然后从table_2中查找ID值。

除了合并,我还没有真正尝试过其他任何东西 但合并保留了table_2中所有额外的40个variables,这不是我喜欢的。

谢谢,SE

最简单的方法是在merge语句中使用keep选项。

 data result; merge table_1 (in=a) table_2 (in=b keep=id definition); by id; if a; run; 

另一个意思是你不必对数据集进行sorting就是使用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; 

下面是一个非常有用(通常非常快)的方法,专门用于1:1匹配,这就是VLOOKUP所做的。 使用匹配variables和查找结果创build格式或信息,并将匹配variablesputinput到主表中。

 data class_income; set sashelp.class(keep=name); income = ceil(12*ranuni(7)); run; data for_format; set class_income end=eof; retain fmtname 'INCOMEI'; start=name; label=income; type='i'; *i=informat numeric, j=informat character, n=format numeric, c=format character; output; if eof then do; hlo='o'; *hlo contains some flags, o means OTHER for nonmatching records; start=' '; label=.; output; end; run; proc format cntlin=for_format; quit; data class; set sashelp.class; income = input(name,INCOMEI.); run;