SAS的持续时间

时间:2015-03-20 11:21:27

标签: sas duration

我有一个关于SAS的问题以及对某个变量状态持续时间的分析。我想知道我的数据集中的每个人有多长时间处于状态,直到状态b发生。如果状态c在状态a 之后出现,则持续时间应设置为零。请注意,如果pre_period处于状态a,我也会将持续时间设置为零,但如果我得到另一个状态,则应该计算其后的状态。

数据看起来像这样:

    pre_period    week1 week2 week3 week4 week5 week6 week7 ...
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     ...

以sas代码设置的示例:

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
;

因此对于id1应该给我持续时间3,对于id2 1,对于id3 3和1,对于id4 5对于id5 1和2以及对于id6 7。

因此输出应该看起来像这样:

    dur1 dur2 dur3 dur4 ...
id1 3    .    .    .    ...
id2 1    .    .    .    ...
id3 3    1    .    .    ...
id4 5    .    .    .    ...
id5 1    2    .    .    ...
id6 7    .    .    .    ...

我是SAS的初学者,并没有找到解决这个问题的方法。请注意,数据集包含几千行和大约一千列,因此对于一个人,我可能有几个状态区间a,我想要捕获它(因此输出中有几个持续时间变量)。

我很感激任何建议。谢谢!

1 个答案:

答案 0 :(得分:0)

在这些情况下,可以明智地考虑有限状态机。通过这种方式,如果您的需求发生变化,以后可以很容易地扩展状态机。

持续时间在三种情况下有效(包括从结果集中给出的隐含的一种):

  • 如果,则应计算状态a的持续持续时间
    • 以状态b
    • 结束
    • 数据集结束时仍处于状态a
    • 并且只要在前期状态为a的第一周内没有开始。

首先,我们必须处理前期要求,我们可以将此状态称为pre_period_locked_state

    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

        if current_state = no_duration_state then do;
            if 'a' = week_state then do;
                 current_state = duration_state;
            end;
        end;

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

        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

        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以外的状态结束的持续时间。

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代码状态机:

 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

+-----+------+------+------+------+------+------+------+
| id  | dur1 | dur2 | dur3 | dur4 | dur5 | dur6 | dur7 |
+-----+------+------+------+------+------+------+------+
| id1 |    3 |      |      |      |      |      |      |
| id2 |    1 |      |      |      |      |      |      |
| id3 |    2 |    2 |      |      |      |      |      |
| id4 |    5 |      |      |      |      |      |      |
| id5 |    1 |    2 |      |      |      |      |      |
| id6 |    7 |      |      |      |      |      |      |
| id7 |    4 |      |      |      |      |      |      |
+-----+------+------+------+------+------+------+------+