Announcement

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

  • Adding new rows to dataset

    Hi

    I have the following data that provides information on natural disasters, their start/end dates and total affected.

    Code:
    input str22 disastertype int(startyear endyear) long totalaffected
    "Earthquake"          1980 1980   930317
    "Drought"             1983 1985 12500000
    "Drought"             1983 1985  2100000
    "Drought"             1980 1985   500000
    "Flood"               1980 1980    30000
    I want a count of how many disasters happened in a year. For example, in 1980, we have 3 events (earthquake, drought and flood). For 1984 we have 3 events (2 droughts that lasted from 1983-85 and one from 1980-85).

    As far as I can see, to be able to count the number of events a year, I need to expand the dataset by adding rows to it, to reflect natural disasters that lasted beyond a year. So the first drought in the dataset that lasted from 1983-1985, two extra rows need to be added with the following information

    Code:
    "Drought" 1984 1985 1250000
    "Drought" 1985 1985 1250000
    For the third drought entry the following rows will be added:

    Code:
    "Drought" 1981 1985 500000
    "Drought" 1982 1985 500000
    "Drought" 1983 1985 500000
    "Drought" 1984 1985 500000
    "Drought" 1985 1985 500000
    Can anyone kindly suggest how I may add new rows as required? Thanks!

  • #2
    Code:
    gen `c(obs_t)' obs_no = _n
    expand endyear-startyear+1
    by obs_no, sort: gen int year = startyear + _n - 1
    by year, sort: gen n_disasters = _N

    Comment


    • #3
      Thank you! Much appreciated

      Comment

      Working...
      X