2013-06-26 52 views
1

我正在寻找一种与Excel中的“VLOOKUP”功能一样工作的SAS代码。与Excel的“VLOOKUP”功能类似的SAS代码

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

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

感谢,SE

回答

4

最简单的方法是使用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; 
+0

我coudn't得到这个工作。你能解释一下sql程序的选择选项中的a和b是什么? –

1

这是一个非常有用的(而且往往非常快)方法,具体为1:1个配套,这是什么VLOOKUP确实。您可以使用匹配变量和查找结果创建格式或信息,并在主表中使用putinput匹配变量。

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;