Re: [程式] SAS資料計算
借用liton的語法
我把他改出來了
稍微修正就可以了
data a;
set a;
retain o 0;
if response^=lag(response) then o=o+1;
run;
proc sql;
create table final as
select date,response,
(case when response^=1 and min(date)^=max(date) then max(date)-min(date)+1
else . end)
as days
from a
group by o ;
quit;
※ 引述《liton (歐吉桑留學生)》之銘言:
: 我程式的就兩段
: 借用t大的資料
: ※ 引述《tew (咖啡王子)》之銘言:
: : data a;
: : input date date10. response;
: : format date date9. ;
: : cards;
: : 09AUG07 1
: : 23AUG07 1
: : 06SEP07 2
: : 20SEP07 2
: : 05OCT07 3
: : 16OCT07 3
: : 30OCT07 3
: : 29NOV07 1
: : 01JAN08 2
: : 10JAN08 3
: : 19JAN08 3
: : 30JAN08 4
: : 10JUN08 4
: : ;
: : run;
: data c;
: set a;
: retain initial_date group 0;
: format initial_date date9. ;
: if response ne 1 and response ne lag(response) then initial_date=date;
: if response ne lag(response) then group+1;
: tmp_days= date-initial_date+1 ;
: run;
: proc sql;
: create table result as
: select date,response,
: (case when response eq 1 then 0 else max(tmp_days) end)
: as days
: from C
: group by group ;
: quit;
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 134.208.29.183
推
06/28 00:14, , 1F
06/28 00:14, 1F
推
06/28 21:14, , 2F
06/28 21:14, 2F
→
06/28 23:55, , 3F
06/28 23:55, 3F
→
06/29 18:26, , 4F
06/29 18:26, 4F
討論串 (同標題文章)