Announcement

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

  • creating a count variable for multiple entries per year

    I have a pannel dataset that is organized by country, code, year, and Disaster type.
    the disaster variable has 5 kinds of observations "Bio, Hydro, Climat, geo, & Meteo." For example:

    Country Code Year Disaster type
    Algeria DZA 1980 Geophysical
    Algeria DZA 1981 Hydrological
    Algeria DZA 1981 Hydrological
    Algeria DZA 1981 Biological

    I want to create 2 variables one that sums up the biological events for each year and another that sums up the 4 remaining categories, without having to duplicate the year. For example:

    Country Code Year occurrence_h occurance_w
    Algeria DZA 1980 0 1
    Algeria DZA 1981 1 2

    I tried creating dummy variables for each disaster type however I couldn't do anything after that. I would be grateful for any ideas on how I can sort out this problem.

    Thanks in advance

  • #2
    Code:
    bysort Country Year Disaster_type : gen occurrence = _N 
    separate occurrence, by(DIsaster_type) veryshortlabel 
    mvencode `r(varlist)', mv(0)

    Comment


    • #3
      Yostina:
      welcome to this forum.
      Another appraoch might be:
      Code:
      . input str20(Country Code) Year str20(Disaster_type)
      
                        Country                  Code       Year         Disaster_type
        1. Algeria DZA 1980 Geophysical
        2. Algeria DZA 1981 Hydrological
        3. Algeria DZA 1981 Hydrological
        4. Algeria DZA 1981 Biological
        5. end
      
      . encode Country,g( Country_n)
      
      . encode Code ,g( Code_n)
      
      . encode Disaster_type ,g( Disaster_type_n)
      
      . bysort Country_n Year Disaster_type_n :egen wanted=count( Disaster_type_n)
      
      . list
      
           +----------------------------------------------------------------------------------+
           | Country   Code   Year   Disaster_t~e   Countr~n   Code_n   Disaster_t~n   wanted |
           |----------------------------------------------------------------------------------|
        1. | Algeria    DZA   1980    Geophysical    Algeria      DZA    Geophysical        1 |
        2. | Algeria    DZA   1981     Biological    Algeria      DZA     Biological        1 |
        3. | Algeria    DZA   1981   Hydrological    Algeria      DZA   Hydrological        2 |
        4. | Algeria    DZA   1981   Hydrological    Algeria      DZA   Hydrological        2 |
           +----------------------------------------------------------------------------------+
      
      . bysort Country_n Year Disaster_type_n : drop if _n==1 & _N>1
      
      
      . list
      
           +----------------------------------------------------------------------------------+
           | Country   Code   Year   Disaster_t~e   Countr~n   Code_n   Disaster_t~n   wanted |
           |----------------------------------------------------------------------------------|
        1. | Algeria    DZA   1980    Geophysical    Algeria      DZA    Geophysical        1 |
        2. | Algeria    DZA   1981     Biological    Algeria      DZA     Biological        1 |
        3. | Algeria    DZA   1981   Hydrological    Algeria      DZA   Hydrological        2 |
           +----------------------------------------------------------------------------------+
      
      .
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Thank you so much for the help

        Comment

        Working...
        X