Stata 计算连续数

Stata 计算连续数,stata,Stata,我有id和DD为1或0 我想为每个id计算“连续的” id year D CO 1 1990 1 1 1 1991 1 2 1 1992 0 0 1 1993 0 0 1 1994 1 1 1 1995 0 0 1 1996 1 1 1

我有
id
D
<代码>D为1或0

我想为每个
id
计算“连续的”

id  year    D    CO    
1   1990    1    1          
1   1991    1    2         
1   1992    0    0         
1   1993    0    0         
1   1994    1    1         
1   1995    0    0         
1   1996    1    1         
1   1997    1    2         
2   1990    1    1         
2   1991    0    0         
2   1992    0    0         
2   1993    1    1         
2   1994    1    2
2   1995    1    3
连续1计算每个
id
D
中连续1的数量

id  year    D    CO    
1   1990    1    1          
1   1991    1    2         
1   1992    0    0         
1   1993    0    0         
1   1994    1    1         
1   1995    0    0         
1   1996    1    1         
1   1997    1    2         
2   1990    1    1         
2   1991    0    0         
2   1992    0    0         
2   1993    1    1         
2   1994    1    2
2   1995    1    3
我做了一笔流动资金,希望这能成为一块垫脚石

bysort id (year): gen runningsumD=sum(D)
然后我也试过了

bysort id (year): replace CO=D[_n-1]+D if D!=0

但是这又一次没有给我想要的。

现在在Statist和Stata杂志上都有关于类似问题的实质性讨论。了解
搜索
的几个关键字很有帮助,例如,感兴趣的拼写跑步由连续值1定义

因此,在这个问题中,咒语开始的条件是:感兴趣的值为1,之前的值为0,或者是面板的开始。(第二种可能性在编码时很容易忽略。)关节条件会给你一个指示变量,在咒语开始时为1,否则为0。然后,你要做的是在观测处于同一个阶段时,提高该指标

以下是数据示例的示例代码和结果:

clear 
input id  year    D    CO    
1   1990    1    1          
1   1991    1    2         
1   1992    0    0         
1   1993    0    0         
1   1994    1    1         
1   1995    0    0         
1   1996    1    1         
1   1997    1    2         
2   1990    1    1         
2   1991    0    0         
2   1992    0    0         
2   1993    1    1         
2   1994    1    2
2   1995    1    3
end 

bysort id (year) : gen wanted = D == 1 & (_n == 1 | D[_n-1] == 0) 
by id: replace wanted = wanted[_n-1] + 1 if D == 1 & wanted == 0 

list, sepby(id) 

     +-----------------------------+
     | id   year   D   CO   wanted |
     |-----------------------------|
  1. |  1   1990   1    1        1 |
  2. |  1   1991   1    2        2 |
  3. |  1   1992   0    0        0 |
  4. |  1   1993   0    0        0 |
  5. |  1   1994   1    1        1 |
  6. |  1   1995   0    0        0 |
  7. |  1   1996   1    1        1 |
  8. |  1   1997   1    2        2 |
     |-----------------------------|
  9. |  2   1990   1    1        1 |
 10. |  2   1991   0    0        0 |
 11. |  2   1992   0    0        0 |
 12. |  2   1993   1    1        1 |
 13. |  2   1994   1    2        2 |
 14. |  2   1995   1    3        3 |
     +-----------------------------+
阅读和程序列表可能包括

SJ-15-1 dm0079  . . . . . . . . . . . . . . .  Stata tip 123: Spell boundaries
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q1/15   SJ 15(1):319--323                                (no commands)
        shows how to identify spells

SJ-7-2  dm0029  . . . . . . . . . . . . . . Speaking Stata: Identifying spells
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/07   SJ 7(2):249--265                                 (no commands)
        shows how to handle spells with complete control over
        spell specification
.pdf,可在以下网址免费获取

第一个提到的.pdf将在Stata Journal 18(1)的出版物上免费提供

TSSpill
(SSC)是一个基本工具,使用了刚刚引用的2007年论文中描述的原则<因此,code>tsspill为您提供了一个不可预测的搜索术语,用于搜索Statalist讨论


也与相关问题相关

目前,Stata在Statalist和Stata杂志上对类似问题进行了大量讨论。了解
搜索
的几个关键字很有帮助,例如,感兴趣的拼写跑步由连续值1定义

因此,在这个问题中,咒语开始的条件是:感兴趣的值为1,之前的值为0,或者是面板的开始。(第二种可能性在编码时很容易忽略。)关节条件会给你一个指示变量,在咒语开始时为1,否则为0。然后,你要做的是在观测处于同一个阶段时,提高该指标

以下是数据示例的示例代码和结果:

clear 
input id  year    D    CO    
1   1990    1    1          
1   1991    1    2         
1   1992    0    0         
1   1993    0    0         
1   1994    1    1         
1   1995    0    0         
1   1996    1    1         
1   1997    1    2         
2   1990    1    1         
2   1991    0    0         
2   1992    0    0         
2   1993    1    1         
2   1994    1    2
2   1995    1    3
end 

bysort id (year) : gen wanted = D == 1 & (_n == 1 | D[_n-1] == 0) 
by id: replace wanted = wanted[_n-1] + 1 if D == 1 & wanted == 0 

list, sepby(id) 

     +-----------------------------+
     | id   year   D   CO   wanted |
     |-----------------------------|
  1. |  1   1990   1    1        1 |
  2. |  1   1991   1    2        2 |
  3. |  1   1992   0    0        0 |
  4. |  1   1993   0    0        0 |
  5. |  1   1994   1    1        1 |
  6. |  1   1995   0    0        0 |
  7. |  1   1996   1    1        1 |
  8. |  1   1997   1    2        2 |
     |-----------------------------|
  9. |  2   1990   1    1        1 |
 10. |  2   1991   0    0        0 |
 11. |  2   1992   0    0        0 |
 12. |  2   1993   1    1        1 |
 13. |  2   1994   1    2        2 |
 14. |  2   1995   1    3        3 |
     +-----------------------------+
阅读和程序列表可能包括

SJ-15-1 dm0079  . . . . . . . . . . . . . . .  Stata tip 123: Spell boundaries
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q1/15   SJ 15(1):319--323                                (no commands)
        shows how to identify spells

SJ-7-2  dm0029  . . . . . . . . . . . . . . Speaking Stata: Identifying spells
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/07   SJ 7(2):249--265                                 (no commands)
        shows how to handle spells with complete control over
        spell specification
.pdf,可在以下网址免费获取

第一个提到的.pdf将在Stata Journal 18(1)的出版物上免费提供

TSSpill
(SSC)是一个基本工具,使用了刚刚引用的2007年论文中描述的原则<因此,code>tsspill为您提供了一个不可预测的搜索术语,用于搜索Statalist讨论


也与相关问题相关

您的第二条语句在正确的行上,但是您是如何初始化
CO
?您的第二条语句在正确的行上,但是您是如何初始化
CO