我需要創建一個新的變量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