2015-07-20 192 views
0

我正在合并SAS,“set_x”和“set_y”中的两个数据集,并且想要在生成的合并数据集“匹配”中创建变量“E”:合并并在同一步骤中创建合并数据集中的变量

* Create set_x *; 
data set_x ;    
input merge_key A B ; 
datalines;    
1 24 25 
2 25 25 
3 30 32 
4 32 32 
5 20 32 
6 1 1 
; 
run; 

* Create set_y *; 
data set_y ;    
input merge_key C D ; 
datalines;    
1 1 1 
2 2 1 
3 1 1 
4 2 1 
5 1 1 
7 1 1 
; 
run; 

* Merge and create E *; 
data matched unmatched_x unmatched_y ; 
merge set_x (in=x) set_y (in=y) ; 
by merge_key ; 
if x=y then output matched; 
     else if x then output unmatched_x ; 
     else if y then output unmatched_y ; 
    IF C = 2 THEN DO ; 
     E = A ; 
    END; 
    ELSE DO ; 
    E = floor(B - D) ; 
    END ; 
run ; 

但是,在结果表“匹配”中,E的值缺失。如果我只输出匹配值(即使用if x=y;),则E被正确计算。

如果输出不匹配和匹配的观测值,是否可以在同一数据步骤中创建“E”?

回答

2

您在计算E之前输出了结果,然后在下一次迭代开始时将E设置为丢失。所以你希望E在输出之前可用,

data matched unmatched_x unmatched_y ; 
merge set_x (in=x) set_y (in=y) ; 
by merge_key ; 
    IF C = 2 THEN DO ; 
     E = A ; 
    END; 

    ELSE DO ; 
    E = floor(B - D) ; 
    END ; 
if x=y then output matched; 
     else if x then output unmatched_x ; 
     else if y then output unmatched_y ; 

run ;