Announcement

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

  • How to transform a bilateral variable into a unilateral variable

    Dear Stata users,

    I am trying to figure out how to solve the following problem:
    I have bilateral variables (called agreement_1, agreement_2, etc.) that equals 1 when two countries belong to the same agreement. However, I would like to have a dummy variable that indicates whether the second country belongs to a specific agreement no matter if its partner country (country 1) belongs to the agreement or not.
    Here is an example:
    Code:
     input str3 country1 str3 country2 agreement_1 agreement_2
    "BDI" "RWA" 1 0
    "BDI" "ZAF" 0 1
    "FRA" "RWA" 0 0
    "RWA" "BDI" 1 0
    end
    So I want to have a unilateral variable (called agreement_1_country2) which will equal 1 for the first, third and fourth line, meaning whenever the country2 is RWA or BDI since we know that these countries belong to the agreement_1.
    I was thinking of using the command tab to obtain all the distinct values for country2:
    Code:
     tab country2 if agreement_1 == 1
    but then I don't know what to eventually create the new variable agreement_1_country2.

    One thing I want to add, is that I don't necessarily need to generate the unilateral variable in my database, I just need it in memory to create additional variables later in my code using this unilateral variable as a condition.

    Thank you in advance.
    Marie


  • #2
    Several ways to do this, here is one.

    Code:
    levelsof country1, local(X) separate(|) clean
    gen wanted= regexm(country2 , "(`X')")
    Res.:

    Code:
    . l
    
         +----------------------------------------------------+
         | country1   country2   agreem~1   agreem~2   wanted |
         |----------------------------------------------------|
      1. |      BDI        RWA          1          0        1 |
      2. |      BDI        ZAF          0          1        0 |
      3. |      FRA        RWA          0          0        1 |
      4. |      RWA        BDI          1          0        1 |
         +----------------------------------------------------+

    Comment


    • #3
      It appears that I missed this

      However, I would like to have a dummy variable that indicates whether the second country belongs to a specific agreement no matter if its partner country (country 1) belongs to the agreement or not.
      You want first

      Code:
      levelsof country1 if agreement_1, local(X) separate(|) clean

      Comment


      • #4
        Dear Andrew,

        Thank you, it works. There was just a small mistake in the code, instead of country1, I used country2:
        Code:
        levelsof country2 if agreement_1, local(X) separate(|) clean
        .

        Best

        Comment

        Working...
        X