Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • generate duration variable

    Dear statalisters,

    I've encountered a problem related to generating duration variable.
    I already search answers of similar questions in statlist, but I can not solve this problem.
    I want to make a 'peaceyear' variable like below Table.
    dyad year dispute peaceyear
    2020 1950 0 1
    2020 1951 0 2
    2020 1952 0 3
    2020 1953 1 4
    2020 1954 0 0
    2020 1955 0 1
    2020 1956 0 2

    Of course, I attemped the code in statalist.
    The code is

    gen peaceyear = .
    bysort dyad(year): replace peaceyear = cond(dispute==0, cond(_n>1, peaceyear[_n-1]+1, 1),1)

    However, I can not make the variable correctly.
    This is my result.
    dyad year dispute peaceyear
    2020 1950 0 1
    2020 1951 0 2
    2020 1952 0 3
    2020 1954 1 1
    2020 1955 0 2
    2020 1956 0 3
    Please notice what's wrong my state code.
    If it is difficult, I will use excel and make peaceyear variable.
    Please help me.
    Thank you.

    regards,
    Youngsang Lee
    Yonsei univ.



  • #2
    I don't understand your rules. Your first year with no dispute is given peace year 1 and 0 in different observations and a year with a dispute is labelled peace year 4.

    Compare these results.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(dyad year) byte(dispute peaceyear)
    2020 1950 0 1
    2020 1951 0 2
    2020 1952 0 3
    2020 1953 1 4
    2020 1954 0 0
    2020 1955 0 1
    2020 1956 0 2
    end
    
    gen wanted = dispute == 0
    bysort dyad (year) : replace wanted = max(0, wanted[_n-1]) + 1 if wanted == 1 & wanted[_n-1] >= 1
    
    list, sepby(dyad dispute)
    
    
         +-------------------------------------------+
         | dyad   year   dispute   peacey~r   wanted |
         |-------------------------------------------|
      1. | 2020   1950         0          1        1 |
      2. | 2020   1951         0          2        2 |
      3. | 2020   1952         0          3        3 |
         |-------------------------------------------|
      4. | 2020   1953         1          4        0 |
         |-------------------------------------------|
      5. | 2020   1954         0          0        1 |
      6. | 2020   1955         0          1        2 |
      7. | 2020   1956         0          2        3 |
         +-------------------------------------------+

    Another approach is to use tsspell from SSC. tsspell is an otherwise unpredictable search term for many mentions of similar problems on Statalist. Here is how that works.

    Code:
    . tsset dyad year
    . tsspell, cond(dispute == 0)
    tsspell creates several variables, of which _seq is identical to wanted above.

    See also https://www.stata-journal.com/articl...article=dm0029 for a more general discussion.

    Comment

    Working...
    X