SAS中的状态持续时间

admin

在这些情况下,它可能是明智的,从有限状态机的角度来思考。这样,如果您的需求发生变化,稍后很容易扩展状态机。

持续时间在三种情况下都是有效的(包括从您的结果集中得到的结果):

状态a的连续持续时间应计算为以状态b结束,当数据集结束时,它仍然处于状态a,只要它不开始在第一周,当前期状态是a。

首先,我们必须处理周期前的需求,我们可以将这个状态称为pre_period_locked_state

代码语言:javascript运行复制 do week = 1 to last_week;

if current_state = pre_period_locked_state then do;

if 'a' not = pre_period or 'a' not = week_state then do;

current_state = duration_state;

end;下一件事是不完美的,当状态不是a时,这里称为no_duration_state

代码语言:javascript运行复制 if current_state = no_duration_state then do;

if 'a' = week_state then do;

current_state = duration_state;

end;

end;这是我们的空闲状态,只有在新的持续时间开始时才会更改。下一个状态名为duration_state,定义为:

代码语言:javascript运行复制 if current_state = duration_state then do;

if 'a' = week_state then do;

duration_count = duration_count + 1;

end;

if ('a' not = week_state or week = last_week) and 0 < duration_count then do;

current_state = dispatch_state;

end;

end;第一部分可能是相当自我声明,持续时间计数器。第二部分讨论持续时间结束的时间。

现在转到dispatch_state

代码语言:javascript运行复制 if current_state = dispatch_state then do;

if 'b' = week_state or 'a' = week_state and week = last_week then do;

duration{duration_index} = duration_count;

duration_index = duration_index + 1;

end;

duration_count = 0;

current_state = no_duration_state;

end;这将负责输出表的索引,并确保只存储有效的持续时间。

我在下面添加了id7,因为示例数据没有以b以外的状态结束的任何持续时间。

代码语言:javascript运行复制data work.sample_data;

input id $ pre_period $ (week1-week7) ($);

datalines;

id1 b b a a a b c c

id2 a a a a b a b b

id3 b b a a b a a b

id4 c c c a a a a a

id5 a b a b b a a b

id6 b a a a a a a a

id7 b a a c a a a a

;完整的sas代码状态机:

代码语言:javascript运行复制 data work.duration_fsm;

set work.sample_data;

array weeks{*} week1-week7;

array duration{*} dur1-dur7;

*states;

initial_reset_state = 'initial_reset_state';

pre_period_locked_state = 'pre_period_locked_state';

duration_state = 'duration_state';

no_duration_state = 'no_duration_state';

dispatch_state = 'dispatch_state';

length current_state $ 50;

*initial values;

current_state = initial_reset_state;

last_week = dim(weeks);

keep id dur1-dur7;

do week = 1 to last_week;

if current_state = initial_reset_state then do;

duration_count = 0;

duration_index = 1;

current_state = pre_period_locked_state;

end;

week_state = weeks{week};

if current_state = pre_period_locked_state then do;

if 'a' not = pre_period and 'a' = week_state then do;

current_state = duration_state;

end;

else if 'a' = pre_period and 'a' not = week_state then do;

current_state = no_duration_state;

end;

end;

if current_state = no_duration_state then do;

if 'a' = week_state then do;

current_state = duration_state;

end;

end;

if current_state = duration_state then do;

if 'a' = week_state then do;

duration_count = duration_count + 1;

end;

if ('a' not = week_state or week = last_week) and 0 < duration_count then do;

current_state = dispatch_state;

end;

end;

if current_state = dispatch_state then do;

if 'b' = week_state or 'a' = week_state and week = last_week then do;

duration{duration_index} = duration_count;

duration_index = duration_index + 1;

end;

duration_count = 0;

current_state = no_duration_state;

end;

end;

run;这将输出work.duration_fsm

代码语言:javascript运行复制+-----+------+------+------+------+------+------+------+

| id | dur1 | dur2 | dur3 | dur4 | dur5 | dur6 | dur7 |

+-----+------+------+------+------+------+------+------+

| id1 | 3 | | | | | | |

| id2 | 1 | | | | | | |

| id3 | 2 | 2 | | | | | |

| id4 | 5 | | | | | | |

| id5 | 1 | 2 | | | | | |

| id6 | 7 | | | | | | |

| id7 | 4 | | | | | | |

+-----+------+------+------+------+------+------+------+

Copyright © 2088 南美洲世界杯预选赛程_世界杯2 - ycfcjt.com All Rights Reserved.
友情链接