Announcement

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

  • How to replace the value of a variable between different years when the years is not fixed numbers and I don't know the value of the years?

    Hello. The graph is snipped from a large dataset. I want to change the variable named havePTA from 1 to 0 the year after one of the countries in the country pair withdraws the trade agreement, and I want to keep the havePTA equal to 1 after the same country pair signs another trade agreement. How can I achieve the goal? For example, in the graph, I want to change havePTA to 0 in the year 2008-2014. The example is easy to do. But the year withdrawal equals 1 for the whole dataset, which is not a fixed number. It is often the case that one of the countries in the country pair withdraws several agreements in several years. For the same reason, the year the country pair signs a new agreement again is not a fixed number, too.

    Explanation of variables:
    year: calendar year
    ISO1: country name
    ISO2: country name
    number: Unique ID for each trade agreement in the dataset
    withdrawl: dummy variable equals 1 if one of the countries of the country pair withdraws the agreement in the year
    pair_id: country id
    year_sign: the year when the country pair sign or withdraw a trade agreement
    havePTA: dummy variable equals 1 if both of the countries of the country pair in a trade agreement


    Attached Files

  • #2
    For your future posts, please familiarize yourself with the dataex command for presenting data examples (see FAQ Advice #12 for details). The following is not tested and will create the wanted variable as opposed to replacing observations in an existing variable.

    Code:
    *ASSERT ISO1 ISO2 YEAR UNIQUELY IDENTIFY OBSERVATIONS
    isid ISO1 ISO2 year
    *CREATE VARIABLE
    bys ISO ISO2 (year): gen wanted= cond(!missing(year_sign) & withdrawal, 0, cond(!missing(year_sign) & !withdrawal, 1, .))
    by ISO1 ISO2: replace wanted= wanted[_n-1] if missing(wanted) & !missing(wanted[_n-1])

    Comment


    • #3
      Thank you. It works.

      Comment

      Working...
      X