我需要创建一个新的变量WHLDR给出以下条件。我不确定最后的其他情况是否正确。所以如果多> 1和ref_1 = 0,如果rel = 0且ref_1 = 1,那么符合此条件的第一个id如果不是whldr = 0,则whldr = 1,并继续。这是我的代码和示例数据如下。使用第一个函数
data temp_all;
merge temp_1 (in=inA)
temp_2 (in=inB)
temp_3 (in=inC)
;
by id;
firstid=first.id;
if multi = 1 then do;
if rel = 0 then whldr=1;
else whldr = 0;
end;
else if multi > 1 and ref_1 >= 1 then do;
if rel =0 and ref_1=1 then whldr=1;
else whldr = 0;
end;
else if multi > 1 and ref_1 = 0 then do;
if rel =0 and ref_1=1 then do;
if rel =0 and ref_0 ne '0' then do;
if first.id=1 then whldr=1 ;
else whldr=0;
end;
end;
end;
run;
这里是样本数据:
data have ;
input id a rel b multi ;
cards;
105 . 0 0 1
110 1 0 1 1
110 0 1 1 1
110 . 2 1 1
113 1 0 1 1
113 2 1 1 1
113 0 2 1 1
113 0 2 1 1
135 1 0 1 1
135 0 1 1 1
176 1 0 1 1
176 0 1 1 1
189 1 0 1 1
189 2 1 1 1
189 0 4 1 1
189 0 4 1 1
;
你需要更详细地解释你想要什么。例如,你如何定义一个人?你想要整个数据集中的第一个人吗?或者在一些其他分组变量中,如状态?发布示例输入和结果数据将有助于澄清你想要的内容。 – Tom
那就是我上面的代码。这些人比较多,我希望整个数据集中的第一人符合最后的条件。我有一个人级别的数据,我试图让第一个人(B)符合这个条件,结果将是一个家庭级别的数据集。 – user601828
如果没有关于您想要的更多信息,我无法评估您的复杂if/then结构以查看它是否正确。但是,如果它正在标记您想要的观察结果,那么您可能只需要添加一个'output;'和'stop;'语句,以便获得一个观察结果数据集。 – Tom