[英]Calculate duration between dates by group using SAS
我試圖計算一個孩子在寄養中的時間。 但是,我遇到了一些問題。 我的數據應如下所示:
對於每個人(ID),我需要計算持續時間(end_date-start_date)。 但是,我還需要應用一條規則,即如果在同一類型的寄養中,結束日期和開始日期之間的時間少於 5 天,則應視為連續安置。 如果同一個人在同一類型的寄養中的結束日期和開始日期之間有超過五天的時間,則這是一個新的安置。 如果是新型寄養,那就是新安置。 變量“持續時間”是應該如何計算的。
我嘗試了以下代碼,但它不能以正確的方式工作 + 我不知道如何應用我的“五天”規則。
Proc sort data=have out=want;
by id type descending start_date;
run;
Data want;
set want;
by id type;
retain last_date;
if first.id or first.type then do;
last_date=end_date;
end;
if last.id or last.type then duration=(end_date-start_date);
run;
任何幫助深表感謝!
在這里使用一堆保留語句來實現這一點:
data want;
set have;
by id ;
retain true_sd prev_ed prev_type;
if first.id then call missing(prev_type);
if type ~= prev_type then do;
true_sd = sd;
call missing(prev_ed);
call missing(prev_type);
end;
if sd - prev_ed > 5 then true_sd = sd;
duration = ed - true_sd;
output;
prev_type = type;
prev_ed = ed;
format sd ed true_sd prev_ed date.;
run;
(假設 type 和 id 在這里是數字。ed 是 end_date,sd 是 start_date)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.