Announcement

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

  • Problem with the Stata Graph

    Hey guys,

    I'm quit new to Stata and want to give out a graph in Stata. I have import and export Data for a few countries in a few periods. I want to show a graph of one country with bars for import and exports. Currently my graph looks crazy and I'm sure a have a few mistakes in my do-file. But I am not sure what exactly is wrong. Hope someone of you stata experts can help me.
    Thanks in advance

    Qubert
    Attached Files

  • #2
    Welcome to Statalist, Qubert! Please go through the Statalist FAQ which has many pointers on how to pose questions effectively. In particular, you should do two things: post your code here using CODE delimiters (the # button on the toolbar in the text box), and post an extract of your data using the command -dataex- in Stata. Screenshots (except that of the graph itself) are not all that helpful for others who are trying to diagnose problems and fix your code.

    Comment


    • #3
      Hemanshu Kumar is naturally bang on. Please follow the advice in #2.

      I can't read the code either, but it seems that you have drawn a line graph when you say that you want bars.

      Comment


      • #4
        Hey Hermanshu, Hey Nick,
        thanks for your advices. I'll try
        My operating system is Mac and my Stata Version is 17
        Below you find the dataex-Code extract and my Do-File. Hope I did it right.

        Thanks in advance and best regards


        dataex----------------------- copy starting from the next line -----------------------
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input int year long period str10 tradeflow str14 reporter str11 partner double tradevalueus float period_new
        2019 201906 "imports"   "united kingdom" "belgium" 2356694934  6
        2019 201907 "imports"   "united kingdom" "belgium" 2598363357  7
        2019 201911 "imports"   "united kingdom" "belgium" 2301624561 11
        2019 201909 "exports"   "united kingdom" "belgium" 1389925243  9
        2019 201902 "imports"   "united kingdom" "belgium" 3037358636  2
        2019 201903 "imports"   "united kingdom" "belgium" 3466473795  3
        2019 201908 "exports"   "united kingdom" "belgium" 1109607898  8
        2019 201910 "exports"   "united kingdom" "belgium" 1555220225 10
        2019 201906 "exports"   "united kingdom" "belgium" 1342127516  6
        2019 201904 "imports"   "united kingdom" "belgium" 2403264480  4
        2019 201902 "exports"   "united kingdom" "belgium" 1486437485  2
        2019 201905 "exports"   "united kingdom" "belgium" 1587587248  5
        2019 201901 "exports"   "united kingdom" "belgium" 1495376569  1
        2019 201903 "exports"   "united kingdom" "belgium" 1703458074  3
        2019 201912 "imports"   "united kingdom" "belgium" 2223295118 12
        2019 201911 "exports"   "united kingdom" "belgium" 1103589216 11
        2019 201905 "imports"   "united kingdom" "belgium" 2315620809  5
        2019 201907 "exports"   "united kingdom" "belgium" 1264351333  7
        2019 201904 "exports"   "united kingdom" "belgium" 1283133285  4
        2019 201901 "imports"   "united kingdom" "belgium" 2815327697  1
        2019 201910 "imports"   "united kingdom" "belgium" 3107415395 10
        2019 201908 "imports"   "united kingdom" "belgium" 2619344040  8
        2019 201912 "exports"   "united kingdom" "belgium" 1165201243 12
        2019 201909 "imports"   "united kingdom" "belgium" 2949897347  9
        2020 202011 "exports"   "united kingdom" "belgium" 1273365175 23
        2020 202005 "imports"   "united kingdom" "belgium" 1386517654 17
        2020 202012 "exports"   "united kingdom" "belgium" 1241221182 24
        2020 202006 "exports"   "united kingdom" "belgium" 1058626805 18
        2020 202011 "imports"   "united kingdom" "belgium" 3061104364 23
        2020 202009 "exports"   "united kingdom" "belgium" 1204877695 21
        2020 202009 "imports"   "united kingdom" "belgium" 2888203336 21
        2020 202007 "exports"   "united kingdom" "belgium" 1149956896 19
        2020 202005 "exports"   "united kingdom" "belgium"  817803249 17
        2020 202008 "imports"   "united kingdom" "belgium" 2309441812 20
        2020 202006 "imports"   "united kingdom" "belgium" 1967856689 18
        2020 202012 "imports"   "united kingdom" "belgium" 3294508926 24
        2020 202010 "exports"   "united kingdom" "belgium" 1394906146 22
        2020 202007 "imports"   "united kingdom" "belgium" 2029773828 19
        2020 202008 "exports"   "united kingdom" "belgium"  998034226 20
        2020 202010 "imports"   "united kingdom" "belgium" 2791955901 22
        2020 202002 "imports"   "united kingdom" "belgium" 2349157105 14
        2020 202003 "imports"   "united kingdom" "belgium" 2814236849 15
        2020 202001 "imports"   "united kingdom" "belgium" 2384554314 13
        2020 202004 "imports"   "united kingdom" "belgium" 1538845295 16
        2020 202003 "exports"   "united kingdom" "belgium" 1204468225 15
        2020 202002 "exports"   "united kingdom" "belgium" 1248488810 14
        2020 202001 "exports"   "united kingdom" "belgium" 1283858194 13
        2020 202004 "exports"   "united kingdom" "belgium"  761431299 16
        2021 202112 "exports"   "united kingdom" "belgium" 1698244185 36
        2021 202112 "imports"   "united kingdom" "belgium" 2728203659 36
        2021 202111 "exports"   "united kingdom" "belgium" 1890874478 35
        2021 202111 "imports"   "united kingdom" "belgium" 2887029419 35
        2021 202108 "exports"   "united kingdom" "belgium" 1538208377 32
        2021 202106 "exports"   "united kingdom" "belgium" 1619116915 30
        2021 202108 "imports"   "united kingdom" "belgium" 2616318027 32
        2021 202101 "exports"   "united kingdom" "belgium"  850887378 25
        2021 202104 "imports"   "united kingdom" "belgium" 2475426489 28
        2021 202106 "imports"   "united kingdom" "belgium" 2828859847 30
        2021 202102 "imports"   "united kingdom" "belgium" 2151388370 26
        2021 202105 "exports"   "united kingdom" "belgium" 1709780733 29
        2021 202102 "exports"   "united kingdom" "belgium" 1773939695 26
        2021 202110 "imports"   "united kingdom" "belgium" 2899843628 34
        2021 202107 "imports"   "united kingdom" "belgium" 2730647005 31
        2021 202109 "exports"   "united kingdom" "belgium" 1716886789 33
        2021 202101 "imports"   "united kingdom" "belgium" 1870095339 25
        2021 202104 "exports"   "united kingdom" "belgium" 1528463529 28
        2021 202109 "imports"   "united kingdom" "belgium" 2906482088 33
        2021 202103 "exports"   "united kingdom" "belgium" 2058386730 27
        2021 202103 "imports"   "united kingdom" "belgium" 2597404913 27
        2021 202105 "imports"   "united kingdom" "belgium" 2357411417 29
        2021 202110 "exports"   "united kingdom" "belgium" 1835505541 34
        2021 202107 "exports"   "united kingdom" "belgium" 1748230297 31
        2022 202201 "imports"   "united kingdom" "belgium" 2810214418 37
        2022 202202 "imports"   "united kingdom" "belgium" 2768988520 38
        2022 202202 "exports"   "united kingdom" "belgium" 2004987726 38
        2022 202201 "exports"   "united kingdom" "belgium" 1435397606 37
        2019 201906 "imports"   "united kingdom" "germany" 6013477983  6
        2019 201906 "reimports" "united kingdom" "germany"     179785  6
        2019 201907 "reimports" "united kingdom" "germany"     211308  7
        2019 201907 "exports"   "united kingdom" "germany" 3814751108  7
        2019 201907 "imports"   "united kingdom" "germany" 7001838026  7
        2019 201911 "imports"   "united kingdom" "germany" 6716714876 11
        2019 201911 "reimports" "united kingdom" "germany"      44191 11
        2019 201909 "exports"   "united kingdom" "germany" 3818363295  9
        2019 201902 "imports"   "united kingdom" "germany" 7790603650  2
        2019 201902 "reimports" "united kingdom" "germany"      57490  2
        2019 201903 "imports"   "united kingdom" "germany" 9069188184  3
        2019 201908 "exports"   "united kingdom" "germany" 3331659423  8
        2019 201910 "exports"   "united kingdom" "germany" 4152042375 10
        2019 201906 "exports"   "united kingdom" "germany" 3496206918  6
        2019 201904 "imports"   "united kingdom" "germany" 7189949830  4
        2019 201904 "reimports" "united kingdom" "germany"      92719  4
        2019 201902 "exports"   "united kingdom" "germany" 4213456381  2
        2019 201905 "exports"   "united kingdom" "germany" 3915718075  5
        2019 201901 "exports"   "united kingdom" "germany" 4394305322  1
        2019 201903 "exports"   "united kingdom" "germany" 4511701104  3
        2019 201912 "imports"   "united kingdom" "germany" 6095263625 12
        2019 201912 "reimports" "united kingdom" "germany"     176003 12
        2019 201903 "reimports" "united kingdom" "germany"     127764  3
        2019 201905 "imports"   "united kingdom" "germany" 6931577040  5
        end
        ------------------ copy up to and including the previous line ------------------


        and my Do-File:

        *Data import*


        import excel "/Users/nicolasstappen/Desktop/Nico/Studium/Master/5. Semester/Masterarbeit/Daten/all Data in one file nur ab 2019.xlsx", sheet("Trade values") firstrow case(lower)

        codebook // entweder nur codebook oder codebook varname



        drop classification // no information and/or variation
        drop perioddesc // no info that is not within Period already
        drop reportercode // no information and/or variation
        drop partnercode // no added information
        drop tradeflowcode // no added information

        replace tradeflow="exports" if tradeflow=="Exports"
        replace tradeflow="imports" if tradeflow=="Imports"
        replace tradeflow="reimports" if tradeflow=="Re-imports"

        replace reporter="united kingdom" if reporter=="United Kingdom"

        replace partner="belgium" if partner=="Belgium"
        replace partner="france" if partner=="France"
        replace partner="germany" if partner=="Germany"
        replace partner="ireland" if partner=="Ireland"
        replace partner="italy" if partner=="Italy"
        replace partner="netherlands" if partner=="Netherlands"
        replace partner="poland" if partner=="Poland"
        replace partner="spain" if partner=="Spain"
        replace partner="sweden" if partner=="Sweden"
        replace partner="cyprus" if partner=="Cyprus"

        gen period_new=.

        tsset period_new

        replace period_new=1 if period==201901
        replace period_new=2 if period==201902
        replace period_new=3 if period==201903
        replace period_new=4 if period==201904
        replace period_new=5 if period==201905
        replace period_new=6 if period==201906
        replace period_new=7 if period==201907
        replace period_new=8 if period==201908
        replace period_new=9 if period==201909
        replace period_new=10 if period==201910
        replace period_new=11 if period==201911
        replace period_new=12 if period==201912
        replace period_new=13 if period==202001
        replace period_new=14 if period==202002
        replace period_new=15 if period==202003
        replace period_new=16 if period==202004
        replace period_new=17 if period==202005
        replace period_new=18 if period==202006
        replace period_new=19 if period==202007
        replace period_new=20 if period==202008
        replace period_new=21 if period==202009
        replace period_new=22 if period==202010
        replace period_new=23 if period==202011
        replace period_new=24 if period==202012
        replace period_new=25 if period==202101
        replace period_new=26 if period==202102
        replace period_new=27 if period==202103
        replace period_new=28 if period==202104
        replace period_new=29 if period==202105
        replace period_new=30 if period==202106
        replace period_new=31 if period==202107
        replace period_new=32 if period==202108
        replace period_new=33 if period==202109
        replace period_new=34 if period==202110
        replace period_new=35 if period==202111
        replace period_new=36 if period==202112
        replace period_new=37 if period==202201
        replace period_new=38 if period==202202
        replace period_new=39 if period==202203
        replace period_new=40 if period==202204

        format %100.0g tradevalueus // um e's wegzubekommen

        graph twoway line tradevalueus year if = tradeflow=="exports" & partner=="belgium"



        Comment


        • #5
          Thanks for the details.

          Please use your full real name as requested in the FAQ Nicolas Stappen ?

          In passing, you might note that

          Code:
          replace reporter="united kingdom" if reporter=="United Kingdom"
          replace partner="belgium" if partner=="Belgium"
          replace partner="france" if partner=="France"
          replace partner="germany" if partner=="Germany"
          replace partner="ireland" if partner=="Ireland"
          replace partner="italy" if partner=="Italy"
          replace partner="netherlands" if partner=="Netherlands"
          replace partner="poland" if partner=="Poland"
          replace partner="spain" if partner=="Spain"
          replace partner="sweden" if partner=="Sweden"
          replace partner="cyprus" if partner=="Cyprus"
          is equivalent to

          Code:
          replace partner = lower(partner)
          while

          Code:
          gen period_new=.
          replace period_new=1 if period==201901
          replace period_new=2 if period==201902
          replace period_new=3 if period==201903
          replace period_new=4 if period==201904
          replace period_new=5 if period==201905
          replace period_new=6 if period==201906
          replace period_new=7 if period==201907
          replace period_new=8 if period==201908
          replace period_new=9 if period==201909
          replace period_new=10 if period==201910
          replace period_new=11 if period==201911
          replace period_new=12 if period==201912
          replace period_new=13 if period==202001
          replace period_new=14 if period==202002
          replace period_new=15 if period==202003
          replace period_new=16 if period==202004
          replace period_new=17 if period==202005
          replace period_new=18 if period==202006
          replace period_new=19 if period==202007
          replace period_new=20 if period==202008
          replace period_new=21 if period==202009
          replace period_new=22 if period==202010
          replace period_new=23 if period==202011
          replace period_new=24 if period==202012
          replace period_new=25 if period==202101
          replace period_new=26 if period==202102
          replace period_new=27 if period==202103
          replace period_new=28 if period==202104
          replace period_new=29 if period==202105
          replace period_new=30 if period==202106
          replace period_new=31 if period==202107
          replace period_new=32 if period==202108
          replace period_new=33 if period==202109
          replace period_new=34 if period==202110
          replace period_new=35 if period==202111
          replace period_new=36 if period==202112
          replace period_new=37 if period==202201
          replace period_new=38 if period==202202
          replace period_new=39 if period==202203
          replace period_new=40 if period==202204
          is equivalent to

          Code:
          gen mdate = ym(floor(period/100), mod(period, 100)) 
          gen period_new = mdate - ym(2008, 12)
          Your sample code


          Code:
          graph twoway line tradevalueus year if = tradeflow=="exports" & partner=="belgium"
          is evidently an attempt to draw a line chart with several countries all importing from Belgium or exporting to Belgium. Either way, values for different countries will get joined up unless you specify otherwise

          First note that the single = would render the command illegal and you would not have got even the graph in #1 if you had included it.

          It is possible that

          Code:
          graph twoway line tradevalueus year if  tradeflow=="exports" & partner=="belgium" , c(L)
          is enough to fix the problem. See help on connectstyle.

          Note that

          Code:
          tsset period_new
          can't help you as you have repeated observations for each monthly date. Perhaps each (importer, exporter) pair should be declared as a panel. but as, minimally, you have re-imports too, that may not be enough,

          Comment


          • #6
            Thanks Nick Cox

            Code:
             replace partner = lower(partner)
            worked well for me

            Code:
             gen mdate = ym(floor(period/100), mod(period, 100)) gen period_new = mdate - ym(2008, 12)
            didn't work. Obviously I did something wrong. But as I understand it is only a more elegant solution, but mine should work as well?!

            What I want to do with my analysis: I want do measure the impact of Brexit on Trade between UK and EU (representative for that I have data of 10 EU countries + data of UK)

            At first I want to show 2 graphs for the trade values:
            1) The imports and exports from/to UK to Germany (Germany as an example for an EU country with high import/export relationship to UK)
            2) The imports and exports from/to UK to Italy (Italy as an example for an EU country with low import/export relationship to UK)
            I don't need to consider Re-imports as they aren't relevant.
            At the moment it is very hard for me to get into Stata since I'm a complete beginner.

            By the way: I used the "contact us" button below to ask Statalist for another User Name, as you explained in another thread

            Kind regards
            Last edited by Nico Stappen; 11 Oct 2022, 05:00.

            Comment


            • #7
              The code you reported as "didn't work" should be two lines not one. (The forum software does not always play well when you copy and paste from one post to another; sometimes line-break mark-up is ignored.) Otherwise I can't comment on "didn't work" without knowing what that means.

              You don't report on the suggestion about fixing the graph.

              Comment


              • #8
                Code:
                gen mdate = ym(floor(period/100), mod(period, 100))
                gen period_new = mdate - ym(2008, 12)
                gave me this
                ----------------------- copy starting from the next line -----------------------
                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input int year long period str10 tradeflow str14 reporter str11 partner double tradevalueus float(mdate period_new)
                2019 201906 "imports"   "united kingdom" "belgium" 2356694934 713 126
                2019 201907 "imports"   "united kingdom" "belgium" 2598363357 714 127
                2019 201911 "imports"   "united kingdom" "belgium" 2301624561 718 131
                2019 201909 "exports"   "united kingdom" "belgium" 1389925243 716 129
                2019 201902 "imports"   "united kingdom" "belgium" 3037358636 709 122
                2019 201903 "imports"   "united kingdom" "belgium" 3466473795 710 123
                2019 201908 "exports"   "united kingdom" "belgium" 1109607898 715 128
                2019 201910 "exports"   "united kingdom" "belgium" 1555220225 717 130
                2019 201906 "exports"   "united kingdom" "belgium" 1342127516 713 126
                2019 201904 "imports"   "united kingdom" "belgium" 2403264480 711 124
                2019 201902 "exports"   "united kingdom" "belgium" 1486437485 709 122
                2019 201905 "exports"   "united kingdom" "belgium" 1587587248 712 125
                2019 201901 "exports"   "united kingdom" "belgium" 1495376569 708 121
                2019 201903 "exports"   "united kingdom" "belgium" 1703458074 710 123
                2019 201912 "imports"   "united kingdom" "belgium" 2223295118 719 132
                2019 201911 "exports"   "united kingdom" "belgium" 1103589216 718 131
                2019 201905 "imports"   "united kingdom" "belgium" 2315620809 712 125
                2019 201907 "exports"   "united kingdom" "belgium" 1264351333 714 127
                2019 201904 "exports"   "united kingdom" "belgium" 1283133285 711 124
                2019 201901 "imports"   "united kingdom" "belgium" 2815327697 708 121
                2019 201910 "imports"   "united kingdom" "belgium" 3107415395 717 130
                2019 201908 "imports"   "united kingdom" "belgium" 2619344040 715 128
                2019 201912 "exports"   "united kingdom" "belgium" 1165201243 719 132
                2019 201909 "imports"   "united kingdom" "belgium" 2949897347 716 129
                2020 202011 "exports"   "united kingdom" "belgium" 1273365175 730 143
                2020 202005 "imports"   "united kingdom" "belgium" 1386517654 724 137
                2020 202012 "exports"   "united kingdom" "belgium" 1241221182 731 144
                2020 202006 "exports"   "united kingdom" "belgium" 1058626805 725 138
                2020 202011 "imports"   "united kingdom" "belgium" 3061104364 730 143
                2020 202009 "exports"   "united kingdom" "belgium" 1204877695 728 141
                2020 202009 "imports"   "united kingdom" "belgium" 2888203336 728 141
                2020 202007 "exports"   "united kingdom" "belgium" 1149956896 726 139
                2020 202005 "exports"   "united kingdom" "belgium"  817803249 724 137
                2020 202008 "imports"   "united kingdom" "belgium" 2309441812 727 140
                2020 202006 "imports"   "united kingdom" "belgium" 1967856689 725 138
                2020 202012 "imports"   "united kingdom" "belgium" 3294508926 731 144
                2020 202010 "exports"   "united kingdom" "belgium" 1394906146 729 142
                2020 202007 "imports"   "united kingdom" "belgium" 2029773828 726 139
                2020 202008 "exports"   "united kingdom" "belgium"  998034226 727 140
                2020 202010 "imports"   "united kingdom" "belgium" 2791955901 729 142
                2020 202002 "imports"   "united kingdom" "belgium" 2349157105 721 134
                2020 202003 "imports"   "united kingdom" "belgium" 2814236849 722 135
                2020 202001 "imports"   "united kingdom" "belgium" 2384554314 720 133
                2020 202004 "imports"   "united kingdom" "belgium" 1538845295 723 136
                2020 202003 "exports"   "united kingdom" "belgium" 1204468225 722 135
                2020 202002 "exports"   "united kingdom" "belgium" 1248488810 721 134
                2020 202001 "exports"   "united kingdom" "belgium" 1283858194 720 133
                2020 202004 "exports"   "united kingdom" "belgium"  761431299 723 136
                2021 202112 "exports"   "united kingdom" "belgium" 1698244185 743 156
                2021 202112 "imports"   "united kingdom" "belgium" 2728203659 743 156
                2021 202111 "exports"   "united kingdom" "belgium" 1890874478 742 155
                2021 202111 "imports"   "united kingdom" "belgium" 2887029419 742 155
                2021 202108 "exports"   "united kingdom" "belgium" 1538208377 739 152
                2021 202106 "exports"   "united kingdom" "belgium" 1619116915 737 150
                2021 202108 "imports"   "united kingdom" "belgium" 2616318027 739 152
                2021 202101 "exports"   "united kingdom" "belgium"  850887378 732 145
                2021 202104 "imports"   "united kingdom" "belgium" 2475426489 735 148
                2021 202106 "imports"   "united kingdom" "belgium" 2828859847 737 150
                2021 202102 "imports"   "united kingdom" "belgium" 2151388370 733 146
                2021 202105 "exports"   "united kingdom" "belgium" 1709780733 736 149
                2021 202102 "exports"   "united kingdom" "belgium" 1773939695 733 146
                2021 202110 "imports"   "united kingdom" "belgium" 2899843628 741 154
                2021 202107 "imports"   "united kingdom" "belgium" 2730647005 738 151
                2021 202109 "exports"   "united kingdom" "belgium" 1716886789 740 153
                2021 202101 "imports"   "united kingdom" "belgium" 1870095339 732 145
                2021 202104 "exports"   "united kingdom" "belgium" 1528463529 735 148
                2021 202109 "imports"   "united kingdom" "belgium" 2906482088 740 153
                2021 202103 "exports"   "united kingdom" "belgium" 2058386730 734 147
                2021 202103 "imports"   "united kingdom" "belgium" 2597404913 734 147
                2021 202105 "imports"   "united kingdom" "belgium" 2357411417 736 149
                2021 202110 "exports"   "united kingdom" "belgium" 1835505541 741 154
                2021 202107 "exports"   "united kingdom" "belgium" 1748230297 738 151
                2022 202201 "imports"   "united kingdom" "belgium" 2810214418 744 157
                2022 202202 "imports"   "united kingdom" "belgium" 2768988520 745 158
                2022 202202 "exports"   "united kingdom" "belgium" 2004987726 745 158
                2022 202201 "exports"   "united kingdom" "belgium" 1435397606 744 157
                2019 201906 "imports"   "united kingdom" "germany" 6013477983 713 126
                2019 201906 "reimports" "united kingdom" "germany"     179785 713 126
                2019 201907 "reimports" "united kingdom" "germany"     211308 714 127
                2019 201907 "exports"   "united kingdom" "germany" 3814751108 714 127
                2019 201907 "imports"   "united kingdom" "germany" 7001838026 714 127
                2019 201911 "imports"   "united kingdom" "germany" 6716714876 718 131
                2019 201911 "reimports" "united kingdom" "germany"      44191 718 131
                2019 201909 "exports"   "united kingdom" "germany" 3818363295 716 129
                2019 201902 "imports"   "united kingdom" "germany" 7790603650 709 122
                2019 201902 "reimports" "united kingdom" "germany"      57490 709 122
                2019 201903 "imports"   "united kingdom" "germany" 9069188184 710 123
                2019 201908 "exports"   "united kingdom" "germany" 3331659423 715 128
                2019 201910 "exports"   "united kingdom" "germany" 4152042375 717 130
                2019 201906 "exports"   "united kingdom" "germany" 3496206918 713 126
                2019 201904 "imports"   "united kingdom" "germany" 7189949830 711 124
                2019 201904 "reimports" "united kingdom" "germany"      92719 711 124
                2019 201902 "exports"   "united kingdom" "germany" 4213456381 709 122
                2019 201905 "exports"   "united kingdom" "germany" 3915718075 712 125
                2019 201901 "exports"   "united kingdom" "germany" 4394305322 708 121
                2019 201903 "exports"   "united kingdom" "germany" 4511701104 710 123
                2019 201912 "imports"   "united kingdom" "germany" 6095263625 719 132
                2019 201912 "reimports" "united kingdom" "germany"     176003 719 132
                2019 201903 "reimports" "united kingdom" "germany"     127764 710 123
                2019 201905 "imports"   "united kingdom" "germany" 6931577040 712 125
                end
                ------------------ copy up to and including the previous line ------------------


                This is how the graph looks now
                Click image for larger version

Name:	Bildschirmfoto 2022-10-11 um 13.33.53.png
Views:	1
Size:	223.6 KB
ID:	1685060
                Do you have an idea how I can get these graphs? Maybe bar diagram is more appropriate than line graph..
                1) The imports and exports from/to UK to Germany (Germany as an example for an EU country with high import/export relationship to UK)
                2) The imports and exports from/to UK to Italy (Italy as an example for an EU country with low import/export relationship to UK)
                I don't need to consider Re-imports as they aren't relevant.

                Comment


                • #9
                  Two comments.

                  1. In your data example there is this correspondence

                  Code:
                   +-------------------+
                    | period   period~w |
                    |-------------------|
                    | 201901        121 |
                    | 201902        122 |
                    | 201903        123 |
                    | 201904        124 |
                    | 201905        125 |
                    | 201906        126 |
                    | 201907        127 |
                    | 201908        128 |
                    | 201909        129 |
                    | 201910        130 |
                    | 201911        131 |
                    | 201912        132 |
                    | 202001        133 |
                    | 202002        134 |
                    | 202003        135 |
                    | 202004        136 |
                    | 202005        137 |
                    | 202006        138 |
                    | 202007        139 |
                    | 202008        140 |
                    | 202009        141 |
                    | 202010        142 |
                    | 202011        143 |
                    | 202012        144 |
                    | 202101        145 |
                    | 202102        146 |
                    | 202103        147 |
                    | 202104        148 |
                    | 202105        149 |
                    | 202106        150 |
                    | 202107        151 |
                    | 202108        152 |
                    | 202109        153 |
                    | 202110        154 |
                    | 202111        155 |
                    | 202112        156 |
                    | 202201        157 |
                    | 202202        158 |
                    +-------------------+
                  Hence the code you report as not working has created a new variable that would be 1 in January 2009 and increase by 1 every later month. Isn't that what you wanted? The code works (is legal) but may not be what you now want (your decision not mine).

                  2. In #8 there is a dopey graph, but as at the start of this thread you don't show the code used to produce it.

                  But plotting against year is of little or no use for monthly data here. Sorry; I should have seen and explained that earlier, but equally it should be clear enough.

                  Comment


                  • #10
                    Ah okay, but do I need this new variable? With my previous code
                    Code:
                     
                     gen period_new=. replace period_new=1 if period==201901 replace period_new=2 if period==201902 replace period_new=3 if period==201903 replace period_new=4 if period==201904 replace period_new=5 if period==201905 replace period_new=6 if period==201906 replace period_new=7 if period==201907 replace period_new=8 if period==201908 replace period_new=9 if period==201909 replace period_new=10 if period==201910 replace period_new=11 if period==201911 replace period_new=12 if period==201912 replace period_new=13 if period==202001 replace period_new=14 if period==202002 replace period_new=15 if period==202003 replace period_new=16 if period==202004 replace period_new=17 if period==202005 replace period_new=18 if period==202006 replace period_new=19 if period==202007 replace period_new=20 if period==202008 replace period_new=21 if period==202009 replace period_new=22 if period==202010 replace period_new=23 if period==202011 replace period_new=24 if period==202012 replace period_new=25 if period==202101 replace period_new=26 if period==202102 replace period_new=27 if period==202103 replace period_new=28 if period==202104 replace period_new=29 if period==202105 replace period_new=30 if period==202106 replace period_new=31 if period==202107 replace period_new=32 if period==202108 replace period_new=33 if period==202109 replace period_new=34 if period==202110 replace period_new=35 if period==202111 replace period_new=36 if period==202112 replace period_new=37 if period==202201 replace period_new=38 if period==202202 replace period_new=39 if period==202203 replace period_new=40 if period==202204
                    I got a new variable as well. Is there a difference?

                    the graph I got with this Code:
                    Code:
                    graph twoway line tradevalueus year if = tradeflow=="exports" & partner=="belgium"
                    What do you mean with that: " But plotting against year is of little or no use for monthly data here. Sorry; I should have seen and explained that earlier, but equally it should be clear enough."

                    Sorry again for beeing slow on the uptake. Stata is a complete new field to me

                    Comment


                    • #11
                      As said in #5 my code for calculating period_new is equivalent to yours. Ir's much shorter code and shows some functionality that can be useful in Stata. No more, no less.

                      The point remaining about the graph is that you need to plot against the monthly date, or some version thereof, not the year. This isn't a point about Stata, but of statistical or graphical logic. When you plot against the year, the year is the same for up to 12 observations and so the graph line goes up and down within each year, which isn't helpful. To get the usual trace from left to right, the second variable in the line command needs to be period_new or mdate in terms of variables mentioned so far.

                      Comment


                      • #12
                        you mean the second variable in this command
                        Code:
                        graph twoway line tradevalueus year if = tradeflow=="exports" & partner=="belgium"
                        needs to be period_new or mdate?

                        Comment


                        • #13
                          That is indeed what I meant, and as already pointed out in #5 the = sign after if is illegal and should be cut.

                          Comment


                          • #14
                            Hey Nick,

                            if I change it this way my graph looks like this:
                            Click image for larger version

Name:	Bildschirmfoto 2022-10-13 um 10.09.31.png
Views:	1
Size:	546.8 KB
ID:	1685242
                            this is my Do-File now:
                            Code:
                            *Data import*
                            
                            
                            import excel "/Users/nicolasstappen/Desktop/Nico/Studium/Master/5. Semester/Masterarbeit/Daten/all Data in one file nur ab 2019.xlsx", sheet("Trade values") firstrow case(lower)
                            
                            codebook // entweder nur codebook oder codebook varname
                            
                            
                            
                            drop classification // no information and/or variation
                            drop perioddesc // no info that is not within Period already
                            drop reportercode // no information and/or variation
                            drop partnercode // no added information
                            drop tradeflowcode // no added information
                            
                            replace tradeflow="exports" if tradeflow=="Exports"
                            replace tradeflow="imports" if tradeflow=="Imports"
                            replace tradeflow="reimports" if tradeflow=="Re-imports"
                            
                            replace reporter="united kingdom" if reporter=="United Kingdom"
                            
                            replace partner="belgium" if partner=="Belgium"
                            replace partner="france" if partner=="France"
                            replace partner="germany" if partner=="Germany"
                            replace partner="ireland" if partner=="Ireland"
                            replace partner="italy" if partner=="Italy"
                            replace partner="netherlands" if partner=="Netherlands"
                            replace partner="poland" if partner=="Poland"
                            replace partner="spain" if partner=="Spain"
                            replace partner="sweden" if partner=="Sweden"
                            replace partner="cyprus" if partner=="Cyprus"
                            
                            gen period_new=.
                            
                            tsset period_new, monthly
                            
                            replace period_new=1 if period==201901
                            replace period_new=2 if period==201902
                            replace period_new=3 if period==201903
                            replace period_new=4 if period==201904
                            replace period_new=5 if period==201905
                            replace period_new=6 if period==201906
                            replace period_new=7 if period==201907
                            replace period_new=8 if period==201908
                            replace period_new=9 if period==201909
                            replace period_new=10 if period==201910
                            replace period_new=11 if period==201911
                            replace period_new=12 if period==201912
                            replace period_new=13 if period==202001
                            replace period_new=14 if period==202002
                            replace period_new=15 if period==202003
                            replace period_new=16 if period==202004
                            replace period_new=17 if period==202005
                            replace period_new=18 if period==202006
                            replace period_new=19 if period==202007
                            replace period_new=20 if period==202008
                            replace period_new=21 if period==202009
                            replace period_new=22 if period==202010
                            replace period_new=23 if period==202011
                            replace period_new=24 if period==202012
                            replace period_new=25 if period==202101
                            replace period_new=26 if period==202102
                            replace period_new=27 if period==202103
                            replace period_new=28 if period==202104
                            replace period_new=29 if period==202105
                            replace period_new=30 if period==202106
                            replace period_new=31 if period==202107
                            replace period_new=32 if period==202108
                            replace period_new=33 if period==202109
                            replace period_new=34 if period==202110
                            replace period_new=35 if period==202111
                            replace period_new=36 if period==202112
                            replace period_new=37 if period==202201
                            replace period_new=38 if period==202202
                            replace period_new=39 if period==202203
                            replace period_new=40 if period==202204
                            
                            format %100.0g tradevalueus // um e's wegzubekommen
                            
                            graph twoway line tradevalueus period_new if tradeflow=="exports" & partner=="belgium"
                            
                            
                            // bis hier daten cleanup formatierung
                            the bold code is what I changed

                            Comment


                            • #15
                              See #6 for the recommendation of the option c(L) (which must follow a comma).

                              Comment

                              Working...
                              X