Announcement

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

  • A problem with Transposing in STATA 17

    Dear friends,

    I have a wide format of time-series data, and I want to change it to a long format. I tried it by running the "reshape" command. The columns are years, and the values are export values. When I run the code below, I got an error.

    My data (shortened):
    partnername B2000 B2001 B2002 B2003
    Afghanistan 8053.165 6982.9 20231.7 36489
    Albania 61271.75 73205 78789.3 114426
    Algeria 376302.1 421964 510837 573002
    American Samoa 5.213 653.5
    Andorra 59.443 6.783 167.42
    Angola 4808.63 8364.3 10951.1 13890

    My code:


    reshape long B2000 B2001 B2002 B2003 B2004 B2005 B2006 B2007 B2008 B2009 B2010 B2011 B2012 B2013 B2014 B2015 B2016 B2017 B2018 B2019 , i(partnername) j(year)


    Error:

    no xij variables found
    You typed something like reshape wide a b, i(i) j(j).


    What I want to do (shortened):

    Afghanistan 2000 Value1
    Afghanistan 2001 Value2
    Afghanistan 2002 Value3
    .
    .
    .
    .

    Thank you so much,



  • #2
    Code:
    reshape long B, i(partnername) j(year)

    Comment


    • #3
      Dear Ali,

      Your suggestion worked, and I got what I exactly need.

      Thank you so much,

      Best,

      Comment


      • #4
        Dear Friends,

        I have a problem related to transformation. I want to transform my data from wide to long. Here below, I provide a data example. These are the EU and top five European countries' commitments to ACP countries (not all EU members). The final data should include three columns: year-country-value separately. The first code shows what I want to have as a final output. The second is the existing data example. The final product should have 126 rows (21*6). All these separate variables "pukdac2a", "pswedendac2a" must be in one column, as a country-year dyad.

        Thank you so much,
        Best,

        Code:
        2000      UK               4356
        2001      UK               1245
        .....
        ...
        .
        2020      UK               4546
        2000      France         9898
        .
        .
        .
        .
        2929      France         5678
        My Data:

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float year str3 ACP double(peuodatotal_01 pukdac2a_01 pswedendac2a_01 pnetherdac2_01 pgermanydac2a_01 pfrancedac2a_01)
        2000 "ACP" 2028.2800018191338  1556.239991992712 500.19000180624425 1015.1599876694381 1272.1600007414818 1783.4900167714804
        2001 "ACP" 3077.5100133121014 1594.5499947294593 457.38000033050776 1448.4199980068952 1088.2200030088425 1353.4500002954155
        2002 "ACP" 2892.4699990302324  1275.320001391694 471.85000435076654 1448.4900050386786 1389.8500048909336 2840.5199846271425
        2003 "ACP" 3125.1700054313987 1616.2299892790616  670.0299942865968 1223.3700040969998 2432.4199960380793   3392.95003359206
        2004 "ACP"  3394.479999186471 2301.2199820429087  590.1699978839606 1302.0800056830049  1385.639991024509  2938.399983299896
        2005 "ACP"  3669.059970073402  3740.089926186949   710.720005730167 1482.7199975065887 2734.7899378947914  3911.779903044924
        2006 "ACP"  3887.709984958172  5066.460063833743  754.1000004038215  1342.850013434887  3504.959963588044  4308.780059901997
        2007 "ACP"   4280.74003149569 1984.3100101985037   765.760000385344 1667.8500102870166 1940.4199956711382  2220.500027947128
        2008 "ACP"  4679.360007543117 2053.9100089799613  754.5000015441328 1264.0299914442003 2059.1299953982234 1757.1699961684644
        2009 "ACP"  4234.230000317097 2529.4000284131616  726.0899943672121  1128.620004080236  1450.769989464432  2443.830009950325
        2010 "ACP" 4977.0300070643425 2774.6900135353208  697.5400000922382 1323.2700022906065 1520.7000007629395  2721.940005907789
        2011 "ACP"  4112.250028204173  2878.699989395216  927.3199985865504  765.5599987264723 1779.9000015631318  3047.289973177016
        2012 "ACP"  4502.960001386702 2675.1100085843354  690.1299976110458  560.4599967133254 2073.3900201953948 2942.8899604529142
        2013 "ACP"  4122.020003784448   3010.04999374412   723.449989726767 507.12999602407217 1341.6600079126656 1933.5099658947438
        2014 "ACP"   4388.06997368671 3068.1799878720194  650.8899973258376 440.64000254683197 1349.0599933862686 1767.1700043138117
        2015 "ACP"  4239.140036217868 3165.9399662259966  714.3800038751215  394.9900007303804 1772.5900030955672 1955.7199877407402
        2016 "ACP"  4367.560016615316  3030.710007539019  705.4099922236055   445.100000789389 2029.6200067214668 1577.3700056932867
        2017 "ACP"  4495.219987630844 3176.8900199644268  843.7899999842048 445.89000578969717 2198.7999852597713 1792.0899848807603
        2018 "ACP"  4413.849953651428 2841.5300082545727  951.2899980004877  444.2799998726696 2338.9900215957314 2259.9899706784636
        2019 "ACP" 3928.5900175869465 2880.9999677687883   914.859996939078 489.59000341035426 2481.6299945004284  2435.690020546317
        2020 "ACP"  4628.410021334887  2375.720000118017  900.0300006456673  476.7500026077032  2975.740013184026  3454.289992624894
        end
        ------------------ copy up to and including the previous line ------------------

        Listed 21 out of 21 observations

        Last edited by Nihat Mugurtay; 14 Jul 2022, 06:53.

        Comment


        • #5
          I don't understand. Have you tried reshape?

          Comment


          • #6
            Yes Jared Greathouse ,

            I tried to use it. Here, I provide a code and error.

            Code:
             reshape long p, i(acp) j(year)
            variable year already exists
                Data are already long.
            r(110);
            I add a data example for the final output (manually reshaped)


            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float year str14 Donors double acpoda
            2000 "European Union" 2028.2800018191338
            2001 "European Union" 3077.5100133121014
            2002 "European Union" 2892.4699990302324
            2003 "European Union" 3125.1700054313987
            2004 "European Union"  3394.479999186471
            2005 "European Union"  3669.059970073402
            2006 "European Union"  3887.709984958172
            2007 "European Union"   4280.74003149569
            2008 "European Union"  4679.360007543117
            2009 "European Union"  4234.230000317097
            2010 "European Union" 4977.0300070643425
            2011 "European Union"  4112.250028204173
            2012 "European Union"  4502.960001386702
            2013 "European Union"  4122.020003784448
            2014 "European Union"   4388.06997368671
            2015 "European Union"  4239.140036217868
            2016 "European Union"  4367.560016615316
            2017 "European Union"  4495.219987630844
            2018 "European Union"  4413.849953651428
            2019 "European Union" 3928.5900175869465
            2020 "European Union"  4628.410021334887
            2000 "United Kingdom"  1556.239991992712
            2001 "United Kingdom" 1594.5499947294593
            2002 "United Kingdom"  1275.320001391694
            2003 "United Kingdom" 1616.2299892790616
            2004 "United Kingdom" 2301.2199820429087
            2005 "United Kingdom"  3740.089926186949
            2006 "United Kingdom"  5066.460063833743
            2007 "United Kingdom" 1984.3100101985037
            2008 "United Kingdom" 2053.9100089799613
            2009 "United Kingdom" 2529.4000284131616
            2010 "United Kingdom" 2774.6900135353208
            2011 "United Kingdom"  2878.699989395216
            2012 "United Kingdom" 2675.1100085843354
            2013 "United Kingdom"   3010.04999374412
            2014 "United Kingdom" 3068.1799878720194
            2015 "United Kingdom" 3165.9399662259966
            2016 "United Kingdom"  3030.710007539019
            2017 "United Kingdom" 3176.8900199644268
            2018 "United Kingdom" 2841.5300082545727
            2019 "United Kingdom" 2880.9999677687883
            2020 "United Kingdom"  2375.720000118017
            2000 "Sweden"         500.19000180624425
            2001 "Sweden"         457.38000033050776
            2002 "Sweden"         471.85000435076654
            2003 "Sweden"          670.0299942865968
            2004 "Sweden"          590.1699978839606
            2005 "Sweden"           710.720005730167
            2006 "Sweden"          754.1000004038215
            2007 "Sweden"           765.760000385344
            2008 "Sweden"          754.5000015441328
            2009 "Sweden"          726.0899943672121
            2010 "Sweden"          697.5400000922382
            2011 "Sweden"          927.3199985865504
            2012 "Sweden"          690.1299976110458
            2013 "Sweden"           723.449989726767
            2014 "Sweden"          650.8899973258376
            2015 "Sweden"          714.3800038751215
            2016 "Sweden"          705.4099922236055
            2017 "Sweden"          843.7899999842048
            2018 "Sweden"          951.2899980004877
            2019 "Sweden"           914.859996939078
            2020 "Sweden"          900.0300006456673
            2000 "Netherlands"    1015.1599876694381
            2001 "Netherlands"    1448.4199980068952
            2002 "Netherlands"    1448.4900050386786
            2003 "Netherlands"    1223.3700040969998
            2004 "Netherlands"    1302.0800056830049
            2005 "Netherlands"    1482.7199975065887
            2006 "Netherlands"     1342.850013434887
            2007 "Netherlands"    1667.8500102870166
            2008 "Netherlands"    1264.0299914442003
            2009 "Netherlands"     1128.620004080236
            2010 "Netherlands"    1323.2700022906065
            2011 "Netherlands"     765.5599987264723
            2012 "Netherlands"     560.4599967133254
            2013 "Netherlands"    507.12999602407217
            2014 "Netherlands"    440.64000254683197
            2015 "Netherlands"     394.9900007303804
            2016 "Netherlands"      445.100000789389
            2017 "Netherlands"    445.89000578969717
            2018 "Netherlands"     444.2799998726696
            2019 "Netherlands"    489.59000341035426
            2020 "Netherlands"     476.7500026077032
            2000 "Germany"        1272.1600007414818
            2001 "Germany"        1088.2200030088425
            2002 "Germany"        1389.8500048909336
            2003 "Germany"        2432.4199960380793
            2004 "Germany"         1385.639991024509
            2005 "Germany"        2734.7899378947914
            2006 "Germany"         3504.959963588044
            2007 "Germany"        1940.4199956711382
            2008 "Germany"        2059.1299953982234
            2009 "Germany"         1450.769989464432
            2010 "Germany"        1520.7000007629395
            2011 "Germany"        1779.9000015631318
            2012 "Germany"        2073.3900201953948
            2013 "Germany"        1341.6600079126656
            2014 "Germany"        1349.0599933862686
            2015 "Germany"        1772.5900030955672
            end
            ------------------ copy up to and including the previous line ------------------

            Listed 100 out of 126 observations
            Use the count() option to list more

            Comment


            • #7
              Code:
              reshape long p, i(ACP year) j(which) string

              Comment


              • #8
                Code:
                reshape long p, i(year acp) j(Donors) string

                Comment


                • #9
                  Dear Nick Cox and William Lisowski,

                  Thank you so much,
                  It ran the code and it worked properly. It saved a lot of time.

                  Best,

                  Comment

                  Working...
                  X