Announcement

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

  • Population same values by year for separate units based on one variable

    Hi All,

    I have data that resembles the following:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(country year population1 population2)
      1 1980 100 200
      1 1981 100 200
      1 1982 100 300
      1 1983 100 400
      .    .   .   .
      .    .   .   .
    100 1980   .   .
    100 1981   .   .
    100 1982   .   .
    100 1983   .   .
    end


    In the above, I have population data by country-year (population 1). In the above dataset, what I wish for is country 100 to contain data from the second column (population 2), by year, but to be replaced in population1 for the years 1980-1982. The dataset should finally look like:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(country year population1 population2)
      1 1980 100 200
      1 1981 100 200
      1 1982 100 300
      1 1983 100 400
      .    .   .   .
      .    .   .   .
    100 1980 200   .
    100 1981 200   .
    100 1982 300   .
    100 1983   .   .
    end
    Any help is much appreciated!

    CS


  • #2
    Code:
    bys year: egen ref= max(population2*(country==1))
    replace population1= ref if country==100 & inrange(year, 1980, 1982)

    Comment

    Working...
    X