Announcement

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

  • #16
    Hi Clyde and Nick,

    I want to generate a new variable "wanted" from the following database:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float t byte(county duration) int damage byte post_dmg int wanted
    744 1 0    0 0    0
    745 1 0    0 0    0
    746 1 2  223 1  223
    747 1 0    0 1  223
    752 1 0    0 0    0
    753 1 6 1456 1 1456
    754 1 0    0 1 1456
    760 1 0    0 0    0
    765 1 0    0 0    0
    766 1 0    0 0    0
    767 1 0    0 0    0
    768 1 2  159 1  159
    769 1 0    0 1  159
    773 1 0    0 0    0
    744 2 4  657 1  657
    745 2 0    0 1  657
    746 2 0    0 1  657
    747 2 0    0 1  657
    748 2 0    0 0    0
    753 2 2  146 1  146
    754 2 2  342 1  488
    755 2 0    0 1  342
    765 2 0    0 0    0
    766 2 0    0 0    0
    767 2 0    0 0    0
    768 2 0    0 0    0
    769 2 0    0 0    0
    770 2 0    0 0    0
    end
    format %tm t
    I have generated post_dmg using this code:

    Code:
    xtset county t, monthly
    gen byte original = 1
    tsfill
    
    clonevar countdown = duration 
    by county : replace countdown = max(countdown, L.countdown - 1, 0)
    by county : gen post_dmg = countdown>0
    keep if original == 1
    drop countdown original
    Please help me to write a code for "wanted". Suppose a disaster happens at period t in county i. The cost of damage is x, and the duration of the disaster is n. Then, I want to place x in the following n period until the effect of the disaster disappears. In the meantime, I will add x1 and x2 in the common period if another disaster happens (please see 2022m11).

    Thanks in advance.


    Kind Regards,
    Woahid

    Comment


    • #17
      I don't know if I understand what you are asking for here. And to the extent that I understand what you say in words, your example contradicts it. But perhaps it is the following. And if it isn't, perhaps at least this will get you started in the right direction.

      Code:
      clonevar dmg = damage
      by county (t), sort: replace dmg = dmg[_n-1] + dmg if dmg > 0 & dmg[_n-1] > 0 & !missing(dmg[_n-1])
      by county (t), sort: gen event_num = sum(dmg > 0 & !missing(dmg))
      by county event_num (t), sort: gen wanted = cond(_n <= duration[1], dmg[1], .)

      Comment


      • #18
        Hi Clyde,

        Thank you so much for your prompt response. However, please see the following example,

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float t double county float(duration damage post_damage wanted)
        545 4013  0  0 0  0
        546 4013 10 30 1 30
        547 4013  1  2 1 32
        548 4013  0  0 1 32
        549 4013  0  0 1 30
        550 4013  0  0 1 30
        551 4013  0  0 1 30
        552 4013  0  0 1 30
        553 4013  0  0 1 30
        554 4013  0  0 1 30
        555 4013  0  0 1 30
        end
        format %tm t

        Code:
        xtset county t, monthly
        gen byte original = 1
        tsfill
        
        clonevar dmg = damage
        by county (t), sort: replace dmg = dmg[_n-1] + dmg if dmg > 0 & dmg[_n-1] > 0 & !missing(dmg[_n-1])
        by county (t), sort: gen event_num = sum(dmg > 0 & !missing(dmg))
        by county event_num (t), sort: gen wanted = cond(_n <= duration[1], dmg[1], .)
        
        drop original dmg event_num
        The code does not work when the lifetime of a preceding damage exceeds the lifetime of post damage. Although post_dmg has been generated perfectly. Please help me to fix the problem.

        Comment


        • #19
          I see. In light of this, I think a different approach is needed. I have combined the two example data sets into one to illustrate the approach. The variable I believe you want is here named continuing_damage, and it appears just after county in the resulting data set.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float t double county float(duration damage post_damage wanted)
          744 1 0    0 0    0
          745 1 0    0 0    0
          746 1 2  223 1  223
          747 1 0    0 1  223
          752 1 0    0 0    0
          753 1 6 1456 1 1456
          754 1 0    0 1 1456
          760 1 0    0 0    0
          765 1 0    0 0    0
          766 1 0    0 0    0
          767 1 0    0 0    0
          768 1 2  159 1  159
          769 1 0    0 1  159
          773 1 0    0 0    0
          744 2 4  657 1  657
          745 2 0    0 1  657
          746 2 0    0 1  657
          747 2 0    0 1  657
          748 2 0    0 0    0
          753 2 2  146 1  146
          754 2 2  342 1  488
          755 2 0    0 1  342
          765 2 0    0 0    0
          766 2 0    0 0    0
          767 2 0    0 0    0
          768 2 0    0 0    0
          769 2 0    0 0    0
          770 2 0    0 0    0545 4013  0  0 0  0
          546 4013 10 30 1 30
          547 4013  1  2 1 32
          548 4013  0  0 1 32
          549 4013  0  0 1 30
          550 4013  0  0 1 30
          551 4013  0  0 1 30
          552 4013  0  0 1 30
          553 4013  0  0 1 30
          554 4013  0  0 1 30
          555 4013  0  0 1 30
          end
          format %tm t
          
          xtset county t, monthly
          gen byte original = 1
          tsfill
          
          tempfile copy
          save `copy'
          
          keep if !missing(damage) & damage > 0
          keep county t duration damage
          gen `c(obs_t)' obs_no = _n
          expand duration
          by obs_no, sort: replace t = t[1] + _n - 1
          collapse (sum) continuing_damage = damage, by(county t)
          
          merge 1:1 county t using `copy', assert(match using) nogenerate


          Last edited by Clyde Schechter; 01 Jul 2024, 13:40.

          Comment


          • #20
            Hi Clyde,

            Perfect!. Thank you so much for your cooperation.


            Kind Regards,
            Woahid

            Comment

            Working...
            X