Announcement

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

  • foreach

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year str3(origin destination) float(ltrade lconflict_o lconflict_d)
    2015 "ABW" "AUS"          . 1.3862944 2.3025851
    2015 "ABW" "BEL"   4.430174  2.912351 2.3025851
    2018 "ABW" "BEL"    3.47035  3.871201 3.2580965
    2019 "ABW" "BEL"   5.643151  .6931472  3.178054
    2001 "ABW" "BGR"          . 3.6888795 2.3025851
    2012 "ABW" "BGR"  3.9740584  2.995732  2.995732
    2017 "ABW" "BHS"   2.498645 2.3025851  1.609438
    2019 "ABW" "BMU"  2.1400661 1.3862944  2.772589
    2009 "ABW" "BRA"  12.039488   2.70805  1.609438
    2017 "ABW" "CAN"   9.943563 4.0073333  4.787492
    2010 "ABW" "CHL"    5.39918  2.944439   2.70805
    2014 "ABW" "CHN"   12.01103 1.8718022 1.3862944
    2004 "ABW" "COL"   7.490289 2.1747518 2.1972246
    2014 "ABW" "COL"  10.532575  4.882802 2.0794415
    2020 "ABW" "COL"    9.13134 2.0794415   2.70805
    2009 "ABW" "CUB"          .  3.555348  3.295837
    2011 "ABW" "CUB"          . 2.0794415  2.484907
    2015 "ABW" "DEU"          . 2.1972246 2.3025851
    2020 "ABW" "DOM"   7.695891 1.3862944  .6931472
    2004 "ABW" "ESP"  10.512612  1.609438 2.3025851
    2009 "ABW" "ESP"   5.138149  2.564949  2.484907
    2017 "ABW" "ESP"  3.5122616 2.3025851 2.3025851
    2018 "ABW" "ESP"  4.1237903  1.609438 2.3025851
    2019 "ABW" "ESP"    3.93306  2.995732 3.3322046
    2020 "ABW" "ESP"   4.750897 3.5263605   2.70805
    2008 "ABW" "GBR"  12.215594 2.3025851  1.609438
    2011 "ABW" "GBR"   8.551253  2.484907  3.317816
    2019 "ABW" "GBR"   4.252416 2.3025851 2.3025851
    2017 "ABW" "GRC"   .6365768  .6931472  5.105946
    2007 "ABW" "GRD"   5.356775 2.3025851   2.95491
    2017 "ABW" "GUY"   5.281466 2.2512918   2.70805
    2019 "ABW" "GUY"   5.984385 2.3025851  2.484907
    2018 "ABW" "ISR"          .  2.995732  2.484907
    2016 "ABW" "ITA"   4.272672 2.3025851 2.3025851
    2017 "ABW" "ITA"  2.0761862   2.70805  2.995732
    2014 "ABW" "JAM"   3.592231  .6931472  2.890372
    2015 "ABW" "JAM"   .4317824  .6931472 2.0794415
    2019 "ABW" "JAM"   3.932727 1.3862944  2.995732
    2009 "ABW" "MEX"  10.566823 1.4816046 1.4816046
    2005 "ABW" "NLD"  10.963469  5.231109  4.477337
    2006 "ABW" "NLD"   9.385333 4.7518644  4.304065
    2007 "ABW" "NLD"   8.363364  5.170484  5.838896
    2008 "ABW" "NLD"    9.99617 4.5217886  3.417727
    2009 "ABW" "NLD"  10.623775 4.4426513 1.7917595
    2010 "ABW" "NLD"   8.642346   5.68358  5.910254
    2011 "ABW" "NLD"   9.544036  5.342334  5.755742
    2012 "ABW" "NLD"    8.81578   5.17615 4.2541933
    2014 "ABW" "NLD"   9.128975  4.882802  4.951593
    2015 "ABW" "NLD"   8.990466 4.4485164  5.078294
    2016 "ABW" "NLD"   9.701871  4.204693 4.5152454
    2017 "ABW" "NLD"    9.18637  4.304065  3.178054
    2018 "ABW" "NLD"  11.609614 3.5263605 4.1108737
    2019 "ABW" "NLD"   8.551231  4.925803  5.389072
    2020 "ABW" "NLD"   8.099095 3.8286414  4.021774
    2010 "ABW" "PER"  1.2510473   5.78996  5.290789
    2011 "ABW" "PER"  11.173037  4.624973 4.6395717
    2012 "ABW" "PER"  -.7277386  5.446737  4.060443
    2014 "ABW" "PER"  .44596705 4.0073333  4.248495
    2015 "ABW" "PER"   7.772379  4.919981  4.875197
    2016 "ABW" "PER" -1.1809075  3.678829  2.890372
    2017 "ABW" "PER"          . 3.6888795 3.6888795
    2011 "ABW" "RUS"          .  .6931472 2.2512918
    2016 "ABW" "SYR"          .  2.995732  2.995732
    2015 "ABW" "THA"  4.5891423  2.944439 2.0794415
    2016 "ABW" "THA"   1.419971 2.3025851 2.3025851
    2004 "ABW" "USA"  14.347646 3.4011974 2.6390574
    2005 "ABW" "USA"  14.836447  5.190732  5.322034
    2006 "ABW" "USA"  14.793563 4.6151204  4.204693
    2007 "ABW" "USA"   14.85459  5.583496  5.288267
    2008 "ABW" "USA"  14.918602  1.609438    3.7612
    2009 "ABW" "USA"   14.00742 4.0253515 2.3025851
    2010 "ABW" "USA"   9.616868  4.836282  5.141664
    2011 "ABW" "USA"   14.93233   7.30317  6.650538
    2012 "ABW" "USA"  13.543797  6.047372  5.719656
    2013 "ABW" "USA"  10.477158  4.804021 3.8286414
    2014 "ABW" "USA"   10.09177  5.998937  6.436951
    2015 "ABW" "USA"  10.505176  5.609472  5.335131
    2016 "ABW" "USA"    8.54795  5.133443  5.129899
    2017 "ABW" "USA"   9.117947  5.169916  5.827474
    2018 "ABW" "USA"   9.327619  5.583496  5.116796
    2019 "ABW" "USA"    9.22293  5.010635  5.098035
    2020 "ABW" "USA"   8.744246  4.304065  3.295837
    2010 "ABW" "VEN"   9.854884  4.317488  3.465736
    2012 "ABW" "VEN"   10.26128 2.0794415  1.609438
    2013 "ABW" "VEN"  10.031178 2.3025851   1.94591
    2014 "ABW" "VEN"   9.571469  7.520072  5.210578
    2015 "ABW" "VEN"   8.486908  5.398163  5.347107
    2016 "ABW" "VEN"   9.173629    3.7281 1.3862944
    2018 "ABW" "VEN"   7.929356 4.7458014  4.204693
    2019 "ABW" "VEN"   8.284257  2.995732 4.5196123
    2020 "ABW" "VEN"   8.859817  4.836282 3.9512436
    2009 "ARE" "ARG"    7.90403 2.0794415 2.0794415
    2010 "ARE" "ARG"   9.501249   4.85203  3.367296
    2011 "ARE" "ARG"  10.690032  3.583519 3.2386785
    2012 "ARE" "ARG"  10.830348 2.3025851 2.3025851
    2013 "ARE" "ARG"  10.835363 2.3025851 3.8286414
    2015 "ARE" "ARG"  10.688694 2.9704144   2.70805
    2016 "ARE" "ARG"  11.169586 1.3862944  3.713572
    2017 "ARE" "ARG"  11.187625  3.218876 1.7917595
    2018 "ARE" "ARG"   12.02374  3.919991  .6931472
    end
    format %ty year
    ------------------ copy up to and including the previous line ------------------

    This is part of dataset. I want to analyze the impact of the conflict(lconflict_o lconflict_d) between A (origin) and B (destination) on the ltrade between A and countries (destinaton) except B or ltrade between B and countries ( destinaton) except A in stata.In other words,I want to analyze the trade diversion effect of conflict. the model is gravity model. my time period is year 2001~2020. Now I want to analyze trade diversion effect of the conflict between CHINA(CHN) and USA(USA) .Here is my code:

    generate conflict_dummy = 0
    replace conflict_dummy = 1 if origin == "CHN" & destination == "USA"|origin == "USA" & destination == "CHN"
    local allcountries "AFG ALB DZA ASM AND AGO ATG AZE ARG AUS AUT BHS BHR BGD ARM BRB BEL BMU BTN BOL BIH BWA BRA BLZ IOT SLB VGB BRN BGR MMR BDI BLR KHM CMR CAN CPV CYM CAF LKA TCD CHL CHN CXR CCK COL COM MYT COG COD COK CRI HRV CUB CYP CSK CZE BEN DNK DMA DOM ECU SLV GNQ ETH ERI EST FLK FJI FIN FRA PYF ATF DJI GAB GEO GMB PSE DEU DDR DEU GHA GIB KIR GRC GRL GRD GUM GTM GIN GUY HTI HND HKG HUN ISL IDN IRN IRQ IRL ISR ITA CIV JAM JPN KAZ JOR KEN PRK KOR KWT KGZ LAO LBN LSO LVA LBR LBY LTU LUX MAC MDG MWI MYS MDV MLI MLT MRT MUS MEX MNG MDA MNE MSR MAR MOZ OMN NAM NRU NPL NLD ANT CUW ABW SXM BES NCL VUT NZL NIC NER NGA NIU NFK NOR MNP FSM MHL PLW PAK PAN PNG PRY PER PHL PCN POL PRT GNB TLS QAT ROU RUS RWA BLM SHN KNA AIA LCA SPM VCT SMR STP SAU SEN SRB SYC SLE IND SGP SVK VNM SVN SOM ZAF ZAF ZWE ESP SSD SDN SDN SUR SWZ SWE CHE SYR TJK THA TGO TKL TON TTO ARE TUN TUR TKM TCA TUV UGA UKR MKD SUN EGY GBR TZA USA BFA URY UZB VEN WLF WSM YEM SCG ZMB"
    foreach country in `allcountries' {
    if (origin == "CHN" & destination == `country') | (origin == "USA" & destination == `country') {

    reghdfe ltrade i.country##c.conflict_dummy, absorb( origin destination)
    predict yhat, fitted
    gen diff = ltrade - yhat
    summarize diff
    }
    output using "results_`country'.dta", replace
    }


    But it turns nothing. So please tell me what's wrong with the above code. Thanks in advance.

  • #2
    Try

    Code:
    if (origin == "CHN" & destination == "`country'") | (origin == "USA" & destination == "`country'") {
    notice the "" around country.

    Comment


    • #3
      Originally posted by Justin Niakamal View Post
      Try

      Code:
      if (origin == "CHN" & destination == "`country'") | (origin == "USA" & destination == "`country'") {
      notice the "" around country.
      It's a pity. Nothing has changed.

      Comment


      • #4
        Code:
        if (origin == "CHN" & destination == "`country'" | (origin == "USA" & destination == "`country'")
        as a command will always be interpreted as

        Code:
        if (origin[1] == "CHN" & destination[1] == "`country'") | (origin[1] == "USA" & destination[1] == "`country'")
        which is not what you want.

        So perhaps you want

        Code:
        reghdfe ltrade i.country##c.conflict_dummy, absorb( origin destination) if (origin == "CHN" & destination == "`country'" | (origin == "USA" & destination == "`country'")
        There are further problems ahead. Second time around the loop predict will fail as yhat already exists. gen diff = would fail for a similar reason.


        I don't know what you intend to do with output, which is not a Stata command.

        Perhaps you should back up and explain what you are trying to do.
        Last edited by Nick Cox; 14 Feb 2023, 01:09.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Code:
          if (origin == "CHN" & destination == "`country'" | (origin == "USA" & destination == "`country'")
          as a command will always be interpreted as

          Code:
          if (origin[1] == "CHN" & destination[1] == "`country'") | (origin[1] == "USA" & destination[1] == "`country'")
          which is not what you want.

          So perhaps you want

          Code:
          reghdfe ltrade i.country##c.conflict_dummy, absorb( origin destination) if (origin == "CHN" & destination == "`country'" | (origin == "USA" & destination == "`country'")
          There are further problems ahead. Second time around the loop predict will fail as yhat already exists. gen diff = would fail for a similar reason.


          I don't know what you intend to do with output, which is not a Stata command.

          Perhaps you should back up and explain what you are trying to do.

          foreach country in `allcountries' {

          reghdfe ltrade i.country##c.conflict_dummy , absorb( origin destination) if (origin == "CHN" & destination == "`country'" | (origin == "USA" & destination == "`country'")
          }
          It shows nothing fter using this command. I just want to analyze the impact of conflict between "CHN" and “USA” on the "CHN"'s exports to the other countries and "USA"'s exports to the other countries.

          Comment


          • #6
            Sorry, but I can't add to my previous answer, unless it is to guess that what you're asking for yields only far too small a subset to fit that model.

            Comment


            • #7
              Thank you, Nick.

              Comment

              Working...
              X