|
|
地板

楼主 |
发表于 2010-8-19 20:14:26
|
只看该作者
Re: 一个问题
相当的不简洁 <!-- s:( --><img src="{SMILIES_PATH}/icon_sad.gif" alt=":(" title="Sad" /><!-- s:( -->
9.2下有效
鸣谢ahuige的赞助
[code:2fokac2l]data had;
input id day status;
datalines;
1 0 0
1 1 0
1 2 1
1 3 0
1 4 1
1 5 1
2 0 1
2 1 0
2 2 1
2 3 0
2 4 1
2 5 1
2 6 0
3 0 1
3 1 1
3 2 0
4 0 0
4 1 0
4 2 1
4 3 0
5 0 0
5 1 1
5 2 1
5 3 0
5 4 1
5 5 1
;
/*proc sort*/
data out(drop=status_list: i position length lag_type first_event_flag);
length status_list $500 type $1;
do _n_=1 by 1 until(last.id);
set had;
by id;
status_list=cats(status_list,put(status,1.));
end;
status_list=translate(status_list,',','0');
event_count=0;
do i=1 to count(status_list,',')+1;
status_list_part=scan(status_list,i,',','m');
if status_list_part='1' then do;
call scan(status_list,i,position,length,',','m');
substr(status_list,position,length)='D';
end;
else if not missing(status_list_part) then do;
first_event_flag=min(first_event_flag,i);
event_count+1;
end;
end;
if not missing(first_event_flag) then do;
call scan(status_list,first_event_flag,position,length,',','m');
substr(status_list,position,length)=repeat('F',length);
end;
status_list=translate(status_list,'D',',','R','1');
group=1;
do _n_=1 to _n_;
set had;
type=substr(status_list,_n_,1);
lag_type=lag(type);
if _n_ gt 1 then do;
if lag_type ne type then group+1;
end;
output;
end;
run;
data out(keep=type id censor time2event);
do _n_=1 by 1 until(last.group);
set out;
by id group;
if _n_=1 then start=day;
end;
if type in('F','R') then do;
censor=0;
if group=1 then time2event=0;
else time2event=start-0;
end;
else if type='D' then do;
censor=1;
time2event=day-0;
end;
if type in('F','R') or event_count=0;
run;[/code:2fokac2l] |
|