Announcement

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

  • Defining a matrice as a local macro

    Dear Statalisters,

    I have the following problem: I have data on international conflicts in which the variable side_a takes the form of "Government of country". If there are multiple countries involved these are only separated by a comma, e.g. "Government of India, Government of Nepal". I would like to see which countries were involved in wars in a given year.
    I have to local macros, one representing the ISO3 country codes and one representing the country names, named listscode and listcountry.
    There are two loops I am currently running: The code looks like this:

    Code:
    foreach scode of local listscode{
        gen `scode'=. 
        }
        
    
    local varlist ZWE ZMB ZAF YEM XTX XKX WSM WLF VUT VNM VIR VGB VEN VCT VAT UZB USA URY UMI UKR UGA TZA TWN TUV TUR TUN TTO TON TLS TKM TKL TJK THA TGO TCD TCA SYR SYC SXM SWZ SWE SVN SVK SUR STP SSD SRB SPM SOM SMR SLV SLE SLB SJM SHN SGS SGP SEN SDN SAU RWA RUS ROU REU QAT PYF PSE PRY PRT PRK PRI POL PNG PLW PHL PER PCN PAN PAK OMN NZL NRU NPL NOR NLD NIU NIC NGA NFK NER NCL NAM MYT MYS MWI MUS MTQ MSR MRT MOZ MNP MNG MNE MMR MLT MLI MKD MHL MEX MDV MDG MDA MCO MAR MAF MAC LVA LUX LTU LSO LKA LIE LCA LBY LBR LBN LAO KWT KOR KNA KIR KHM KGZ KEN KAZ JPN JOR JEY JAM ITA ISR ISL IRQ IRN IRL IOT IND IMN IDN HUN HTI HRV HND HMD HKG GUY GUM GUF GTM GRL GRD GRC GNQ GNB GMB GLP GIN GIB GHA GGY GEO GBR GAB FSM FRO FRA FLK FJI FIN ETH EST ESP ESH ERI EGY ECU DZA DOM DNK DMA DJI DEU CZE CYP CYM CXR CUW CUB CRI CPV COM COL COK COG COD CMR CIV CHN CHL CHE CCK CAN CAF BWA BVT BTN BRN BRB BRA BOL BMU BLZ BLR BLM BIH BHS BHR BGR BGD BFA BES BEN BEL BDI AZE AUT AUS ATG ATF ATA ASM ARM ARG ARE AND ALB ALA AIA AGO AFG ABW 
    
    //soweit so gut
    
    foreach var of local varlist {
        foreach country of local listcountry{
        replace `var'=1 if strpos(side_a, `"`country'"')
    }
    }
    Of course what this does is return a 1 for all variables if any country is involved in a war. What I want it to do is basically look at table/matrice of country codes and country names, and if it finds a country name in the variable side_a, change the value of the variable representing the country code to 1. I have looked around the local and macro help files but did not find a solution of connecting two local macro lists like this, or storing a matrice, where I could write code that looks for one column, and than refers to the second column. Really happy for any help!
    I hope I explained myself reasonably well.

    Thanks in advance!
    Kindest regards
    Robert

  • #2
    What is inside the variable side_a? What is inside the locals listscode and listcountry? Can you post here a simplified dataset that allows a full picture?

    Comment


    • #3
      Hi Nick of course:

      levelsof scode, local(listscode)
      levelsof country, local(listcountry)

      di `listscode'

      di `listscode'
      ABW AFG AGO AIA ALA ALB AND ARE ARG ARM ASM ATA ATF ATG AUS AUT AZE BDI (...)

      di `listcountry'
      AfghanistanAland IslandsAlbaniaAlgeriaAmerican SamoaAndorraAngolaAnguillaAntarcticaAntigua (...)

      side_a
      Government of India
      Government of India
      Government of Egypt
      Government of Egypt
      Government of Egypt
      Government of Egypt
      Government of Sudan
      Government of South Sudan
      Government of South Sudan
      Government of South Sudan
      Government of South Sudan
      Government of South Sudan
      Government of South Sudan
      Government of South Sudan
      Government of South Sudan
      Government of South Sudan
      Government of South Sudan
      Government of South Sudan
      Government of Libya

      Does that help you?
      Kindest regards
      Robert

      Comment


      • #4
        The dataset is the UCDO/PRIO Armed Conflict Dataset version 22.1 available at https://ucdp.uu.se/downloads/index.html#armedconflict

        The lists are generated from a dataset that just lists the countries and country codes.

        Kindest regards Robert

        Comment


        • #5
          We need please

          Code:
          di `"`listcountry'"'

          Comment


          • #6
            Here you go
            Code:
            di `"`listcountry'"'
            `"Afghanistan"' `"Aland Islands"' `"Albania"' `"Algeria"' `"American Samoa"' `"Andorra"' `"Angola"' `"Ang
            > uilla"' `"Antarctica"' `"Antigua and Barbuda"' `"Argentina"' `"Armenia"' `"Aruba"' `"Australia"' `"Aust
            > ria"' `"Azerbaijan"' `"Bahamas"' `"Bahrain"'
            Thanks for your patience!

            Comment


            • #7
              Thanks; that is enough information to reconstruct what I think is your problem. You need loops in parallel, not nested loops. For much more discussion, see https://journals.sagepub.com/doi/pdf...6867X211063415

              This seems to capture the essence of the calculation.

              Code:
              clear 
              
              input str26 side_a 
              "Government of Afghanistan"
              "Government of Finland"
              end 
              
              local listscode AFG FIN 
              
              foreach scode of local listscode { 
                  gen `scode'=. 
              }
                  
              local varlist AFG FIN 
              
              local listcountry `" `"Afghanistan"' `"Finland"' "' 
              
              local j = 0 
              foreach var of local varlist {
                  local ++j 
                  local country : word `j' of `listcountry'
                 
                  replace `var'=1 if strpos(side_a, `"`country'"')
              }
              
              list
              but I would strongly recommend (0, 1) indicators not (1, .) indicators so that your line would be


              Code:
              replace `var'= strpos(side_a, `"`country'"') > 0

              Comment


              • #8
                Thanks for your help so far!
                I think this is a good start! However, I don't think this solves the problem.
                The problem is that the first item in the listcountry does not correspond to the first item in listcountrycode.
                Both lists are generated by the levelsof command and these are generated in alphabetical order. Just as an example:
                Code:
                 di `"`listscode'""'
                `"ABW"' `"AFG"' `"AGO"' `"AIA"' `"ALA"'
                di `"`listcountry'"'
                `"Afghanistan"' `"Aland Islands"' `"Albania"' `"Algeria"'
                The country code for Afghanistan is AFG. The loop would have to connect AFG and Afghanistan when generating the local macro. in the first place I think. Or do you think I should just make the varlist in the order of the country's in listcountry? Thanks in advance!

                Comment


                • #9
                  I imagine you do need these lists aligned; otherwise the problem is more difficult.

                  Comment

                  Working...
                  X