Announcement

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

  • Creating spells

    Hello to all!

    First of all, I would like to thank the users of this forum who helped me a lot in my learning of Stata software.

    I'm asking you today to solve a problem with the creation spells. Indeed I have a panel database of countries observed on a monthly basis. The variable disaster is coded as follows: it takes the value 1 when a climatic phenomenon takes place in a specific month. If the disaster lasted more than one month then the variable takes the value of -1 the month it ended.

    I would like to create a new variable (disacter2) from a stata code which will take the value of 1 on the period of phenomenon and zero otherwise. As an illustration I have created this variable (disester2) in Excel.

    I would be grateful if you could help me to create this variable in stata. Thanks



    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year byte month str3 country_code byte(disaster disaster2) float period
    2018  1 "BEN"  0 0 696
    2018  2 "BEN"  0 0 697
    2018  3 "BEN"  0 0 698
    2018  4 "BEN"  0 0 699
    2018  5 "BEN"  0 0 700
    2018  6 "BEN"  1 0 701
    2018  7 "BEN"  0 0 702
    2018  8 "BEN"  0 0 703
    2018  9 "BEN"  0 0 704
    2018 10 "BEN"  0 0 705
    2018 11 "BEN"  0 0 706
    2018 12 "BEN"  0 0 707
    2019  1 "BEN"  0 0 708
    2019  2 "BEN"  0 0 709
    2019  3 "BEN"  0 0 710
    2019  4 "BEN"  0 0 711
    2019  5 "BEN"  1 1 712
    2019  6 "BEN"  0 1 713
    2019  7 "BEN"  0 1 714
    2019  8 "BEN"  0 1 715
    2019  9 "BEN"  0 1 716
    2019 10 "BEN"  0 1 717
    2019 11 "BEN" -1 1 718
    2019 12 "BEN"  0 0 719
    2020  1 "BEN"  0 0 720
    2020  2 "BEN"  0 0 721
    2020  3 "BEN"  0 0 722
    2020  4 "BEN"  0 0 723
    2020  5 "BEN"  0 0 724
    2020  6 "BEN"  0 0 725
    2020  7 "BEN"  0 0 726
    2020  8 "BEN"  0 0 727
    2020  9 "BEN"  0 0 728
    2020 10 "BEN"  0 0 729
    2020 11 "BEN"  1 1 730
    2020 12 "BEN"  0 1 731
    2021  1 "BEN"  0 1 732
    2021  2 "BEN"  0 1 733
    2021  3 "BEN"  0 1 734
    2021  4 "BEN"  0 1 735
    2021  5 "BEN" -1 1 736
    2021  6 "BEN"  0 0 737
    2021  7 "BEN"  0 0 738
    2021  8 "BEN"  0 0 739
    2021  9 "BEN"  0 0 740
    2021 10 "BEN"  0 0 741
    2021 11 "BEN"  0 0 742
    2021 12 "BEN"  1 0 743
    2018  1 "BFA"  0 0 696
    2018  2 "BFA"  0 0 697
    2018  3 "BFA"  0 0 698
    2018  4 "BFA"  0 0 699
    2018  5 "BFA"  0 0 700
    2018  6 "BFA"  1 0 701
    2018  7 "BFA"  0 0 702
    2018  8 "BFA"  0 0 703
    2018  9 "BFA"  0 0 704
    2018 10 "BFA"  0 0 705
    2018 11 "BFA"  0 0 706
    2018 12 "BFA"  1 1 707
    2019  1 "BFA"  0 1 708
    2019  2 "BFA"  0 1 709
    2019  3 "BFA"  0 1 710
    2019  4 "BFA"  0 1 711
    2019  5 "BFA"  0 1 712
    2019  6 "BFA"  0 1 713
    2019  7 "BFA" -1 1 714
    2019  8 "BFA"  0 0 715
    2019  9 "BFA"  0 0 716
    2019 10 "BFA"  0 0 717
    2019 11 "BFA"  0 0 718
    2019 12 "BFA"  0 0 719
    2020  1 "BFA"  0 0 720
    2020  2 "BFA"  0 0 721
    2020  3 "BFA"  1 0 722
    2020  4 "BFA"  0 0 723
    2020  5 "BFA"  0 0 724
    2020  6 "BFA"  0 0 725
    2020  7 "BFA"  0 0 726
    2020  8 "BFA"  1 1 727
    2020  9 "BFA"  0 1 728
    2020 10 "BFA"  0 1 729
    2020 11 "BFA"  0 1 730
    2020 12 "BFA"  0 1 731
    2021  1 "BFA"  0 1 732
    2021  2 "BFA"  0 1 733
    2021  3 "BFA" -1 1 734
    2021  4 "BFA"  0 0 735
    2021  5 "BFA"  0 0 736
    2021  6 "BFA"  0 0 737
    2021  7 "BFA"  0 0 738
    2021  8 "BFA"  0 0 739
    2021  9 "BFA"  0 0 740
    2021 10 "BFA"  0 0 741
    2021 11 "BFA"  0 0 742
    2021 12 "BFA"  0 0 743
    2018  1 "CIV"  1 0 696
    2018  2 "CIV"  0 0 697
    2018  3 "CIV"  0 0 698
    2018  4 "CIV"  0 0 699
    end
    format %tm period
    Attached Files

  • #2
    Code:
    isid country period, sort
    
    frame put country_code disaster period if disaster != 0, into(endpoints)
    
    frame endpoints: by country_code (period), sort: gen int next_endpoint = disaster[_n+1]
    
    frlink 1:1 country_code period, frame(endpoints)
    frget next_endpoint, from(endpoints)
    drop endpoints
    frame drop endpoints
    
    by country_code (period): gen wanted = sum((disaster == 1 & next_endpoint == -1) ///
        - (disaster[_n-1] == -1))

    Comment


    • #3
      Hello Mr. Clyde Schechter,

      I have tried your code and it works perfectly. I would like to thank you very much for your legendary availability and your great support. It is really greatly appreciated! God bless you!!!

      Comment

      Working...
      X