Announcement

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

  • How to assign the same variable value to the same individual in different observations

    Hello. The following "dataset 1" is the dataset I am using. I want to replace year_withdrawal with 2007 in observation 1, replace year_sign with 1993 in observation 2, and replace withdrawalnumber with 243 in obseravtion 1. The final goal is change the "dataset 1" to the "dataset 2"

    dataset1
    clear
    input str5(ISO1 ISO2) float year str6 number int base_treaty float(year_sign year_withdrawal withdrawalnumber)
    "AGO" "BDI" 1993 "243" 243 1993 . .
    "AGO" "BDI" 2007 "243_4" 243 . 2007 243
    "AGO" "COD" 1993 "243" 243 1993 . .
    "AGO" "COD" 2007 "243_4" 243 . 2007 243
    "AGO" "COM" 1993 "243" 243 1993 . .
    "AGO" "COM" 2007 "243_4" 243 . 2007 243
    "AGO" "DJI" 1993 "243" 243 1993 . .
    "AGO" "DJI" 2007 "243_4" 243 . 2007 243
    "AGO" "EGY" 1998 "243+1" 243 1998 . .
    "AGO" "EGY" 2007 "243_4" 243 . 2007 243
    end


    dataset2
    clear
    input str5(ISO1 ISO2) float year str6 number int base_treaty float(year_sign year_withdrawal withdrawalnumber)
    "AGO" "BDI" 1993 "243" 243 1993 2007 243
    "AGO" "BDI" 2007 "243_4" 243 1993 2007 243
    "AGO" "COD" 1993 "243" 243 1993 2007 243
    "AGO" "COD" 2007 "243_4" 243 1993 2007 243
    "AGO" "COM" 1993 "243" 243 1993 2007 243
    "AGO" "COM" 2007 "243_4" 243 1993 2007 243
    "AGO" "DJI" 1993 "243" 243 1993 2007 243
    "AGO" "DJI" 2007 "243_4" 243 1993 2007 243
    "AGO" "EGY" 1998 "243+1" 243 1998 2007 243
    "AGO" "EGY" 2007 "243_4" 243 1998 2007 243
    end


    ISO1: country name
    ISO2:country name
    number: different versions of base_treaty
    base_treaty: number of treaty
    year_sign: the year when ISO1 and ISO2 signed a treaty
    year_withdrawal:the year when ISO1 and ISO2 withdrawal a treaty


  • #2
    Code:
    foreach v of varlist year_sign year_withdrawal withdrawalnumber {
        by ISO1 ISO2 base_treaty (`v'), sort: replace `v' = `v'[1]
    }
    sort ISO1 ISO2 year
    May I ask why you want to do this? The resulting data set is a very odd layout of the data, with the same numbers being repeated both vertically and horizontally. Each pair of observations actually ends up containing exactly the same information except for the variables year and number. There are better ways to organize this data for use in Stata, and organizations like the one you are creating here often get you in trouble later. If you can indicate what you plan to do with this, a layout that will prove more suitable for the purpose can be suggested.

    Comment


    • #3
      Thank you. But before I provide the data, I need your help to close the Stata. I don't know why I can't close the Stata and run new do files.
      Attached Files

      Comment


      • #4
        Thank you for your response yesterday. You asked why I wanted to do this. The data I provided yesterday was from a large dataset containing one million observations. I want to find in which year a treaty exists between two countries. That's why I create year2000, year2001,..., etc., these dummy variables. The year range should be 1945 to 2021,like year1945,year1946. But the "dataex" can't export so many variables. And the base_treaty should be 1 to 1100.
        I want to use the first observation as an example. ISO1 is a country name, ISO2 is a country name, year is the calendar year, base_treaty is the number (or ID) of a treaty in the dataset, the number is the different version of the base_treaty, year sign is the year ISO1 and ISO2 signed the treaty, year withdrawal is the year ISO1 and ISO2 withdrew the treaty, year2000 is 1 means in the year 2000, the treaty exists or in effect.

        clear
        input str5(ISO1 ISO2) float year str6 number int base_treaty float(year_sign year_withdrawal withdrawalnumber year2000 year2001 year2002)
        "AGO" "BDI" 1993 "243" 243 1993 2007 243 1 1 1
        "AGO" "BDI" 2007 "243_4" 243 1993 2007 243 1 1 1
        "AGO" "COD" 1993 "243" 243 1993 2007 243 1 1 1
        "AGO" "COD" 2007 "243_4" 243 1993 2007 243 1 1 1
        "AGO" "COM" 1993 "243" 243 1993 2007 243 1 1 1
        "AGO" "COM" 2007 "243_4" 243 1993 2007 243 1 1 1
        "AGO" "DJI" 1993 "243" 243 1993 2007 243 1 1 1
        "AGO" "DJI" 2007 "243_4" 243 1993 2007 243 1 1 1
        "AGO" "EGY" 1998 "243+1" 243 1998 2007 243 1 1 1
        "AGO" "EGY" 2007 "243_4" 243 1998 2007 243 1 1 1
        end

        Comment


        • #5
          I understand the information you want to be able to access from them. But you haven't explained what you plan to do with these variables once you have them.

          Let me just say that for most analyses in Stata you will find it difficult to make use of those variables. I think you will likely be better off if you organize the data as follows:
          Code:
          clear
          input str5(ISO1 ISO2) float year str6 number int base_treaty float(year_sign year_withdrawal withdrawalnumber)
          "AGO" "BDI" 1993 "243" 243 1993 . .
          "AGO" "BDI" 2007 "243_4" 243 . 2007 243
          "AGO" "COD" 1993 "243" 243 1993 . .
          "AGO" "COD" 2007 "243_4" 243 . 2007 243
          "AGO" "COM" 1993 "243" 243 1993 . .
          "AGO" "COM" 2007 "243_4" 243 . 2007 243
          "AGO" "DJI" 1993 "243" 243 1993 . .
          "AGO" "DJI" 2007 "243_4" 243 . 2007 243
          "AGO" "EGY" 1998 "243+1" 243 1998 . .
          "AGO" "EGY" 2007 "243_4" 243 . 2007 243
          end
          
          by ISO1 ISO2 base_treaty (year), sort: gen pact = 1 if _n == 1, before(ISO1)
          replace pact = sum(pact)
          insobs 1
          replace year = 2021 in L
          insobs 1, after(1)
          replace year = 1947 in 2
          replace pact = pact[_n-1] if inlist(_n, 2, _N)
          
          tsset pact year
          tsfill, full
          
          foreach v of varlist ISO1 ISO2 number {
              by pact (`v'), sort: replace `v' = `v'[_N]
          }
          foreach v of varlist base_treaty year_sign year_withdrawal withdrawalnumber {
              by pact (`v'), sort: replace `v' = `v'[1]
          }
          
          isid pact year, sort
          gen treaty_in_effect = inrange(year, year_sign, year_withdrawal)
          For most analyses and data management in Stata, this will prove more workable than the arrangement you were creating.

          Comment


          • #6
            Thank you.

            Comment

            Working...
            X