Announcement

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

  • How can I create a new variable equal to the logarithm of GDP per capita?

    Hello, I have a database where the years are in the first column. From the second column, there are the names of more than 100 countries (in the first row). Then all the content of the base is the GDP per capita. How can I create a new variable equal to the log of GDP per capita using the reshape command?

    dataex year Austria Belgium

    ----------------------- copy starting from the next line -----------------------
    [CODE]
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year double(Austria Belgium)
    1800 . .
    1801 . .
    1802 . .
    1803 . .
    1804 . .
    1805 . .
    1806 . .
    1807 . .
    1808 . .
    1809 . .
    1810 . .
    1811 . .
    1812 . 1478.9104126809905
    1813 . .
    1814 . .
    1815 . .
    1816 . .
    1817 . .
    1818 . .
    1819 . .
    1820 1218.1656277827249 .
    1821 . .
    1822 . .
    1823 . .
    1824 . .
    1825 . .
    1826 . .
    1827 . .
    1828 . .
    1829 . .
    1830 1398.5302430751838 .
    1831 . .
    1832 . .
    1833 . .
    1834 . .
    1835 . .
    1836 . .
    1837 . .
    1838 . .
    1839 . .
    1840 1514.5317545748117 .
    1841 . .
    1842 . .
    1843 . .
    1844 . .
    1845 . .
    1846 . 1693.9013035381752
    1847 . 1761.5970459266098
    1848 . 1753.6032944406315
    1849 . 1790.3811252268601
    1850 1650.379746835443 1846.7071251966734
    1851 . 1885.637703819522
    1852 . 1923.6573457612071
    1853 . 1961.6232906925452
    1854 . 2069.6909927679158
    1855 . 2070.775261324042
    1856 . 2169.6602466998484
    1857 . 2211.352397333907
    1858 . 2211.538461538462
    1859 . 2197.4522292993634
    1860 1777.567886658796 2292.616033755274
    1861 . 2310.2220360284878
    1862 . 2353.9197338323975
    1863 . 2403.7985136251036
    1864 . 2466.079114572658
    1865 . 2448.0162767039674
    1866 . 2502.626262626263
    1867 . 2497.392699558765
    1868 . 2569.1817638861235
    1869 . 2662.557168423146
    1870 1862.5904867256638 2691.5227629513347
    1871 1979.0957913195969 2682.4648627603656
    1872 1976.3223284100782 2823.6469679412903
    1873 1913.0281962978909 2820.0584403142366
    1874 1980.929180887372 2889.7719064816574
    1875 1973.2553911205073 2860.7850273430136
    1876 2000.116722548198 2874.6164639850326
    1877 2050.4404984423672 2884.4840563589173
    1878 2100.3057442865966 2942.257442116869
    1879 2067.93876301286 2944.919883466861
    1880 2078.837887067395 3064.8213318895505
    1881 2145.166700100301 3069.676061362826
    1882 2139.9620278330017 3136.056760091662
    1883 2208.783842364532 3144.5341463414634
    1884 2248.4751025190394 3136.10487342862
    1885 2215.2706155632986 3137.8131381892445
    1886 2267.7099002302375 3153.2723433012334
    1887 2404.4300932090546 3249.8914793693393
    1888 2379.1510465774095 3247.0852338937902
    1889 2337.2069932685117 3378.5000826309697
    1890 2443.3559510567297 3427.8480971128606
    1891 2506.147998531032 3395.280499675536
    1892 2535.1280886627906 3441.8329321136257
    1893 2525.1031817364733 3455.4811111111117
    1894 2644.598719316969 3468.289010989011
    1895 2687.7818661971833 3511.5006988662844
    1896 2701.0125457396757 3551.4953803510934
    1897 2730.1526388409798 3586.4266951740988
    1898 2855.287397540984 3614.7919442761963
    1899 2886.2769230769236 3656.1529570699


    summarize

    Variable | Obs Mean Std. dev. Min Max
    -------------+---------------------------------------------------------
    year | 211 1905 61.05462 1800 2010
    Austria | 146 7214.719 6679.876 1218.166 24565.48
    Belgium | 166 7308.937 6162.118 1478.91 23701.3
    Denmark | 191 6941.169 6859.326 1273.593 25060.29
    Finland | 153 6313.913 6641.172 781.0094 24694.06
    -------------+---------------------------------------------------------
    France | 191 6177.122 6235.37 1134.976 22202.17
    Germany | 162 6732.91 5965.322 986.3717 20801.44
    Italy | 211 4798.488 5540.265 1332.537 19841.54
    Netherlands | 205 6594.172 6194.154 1639.615 25112.31
    Norway | 182 6543.069 7773.931 767.4737 28555.95
    -------------+---------------------------------------------------------
    Sweden | 211 5749.215 6642.141 766.2757 25376.74
    Switzerland | 160 10151.8 6892.967 1796.391 25293.2
    UnitedKing~m | 211 6726.801 5738.729 1946.974 25002.12
    Ireland | 93 8013.292 6805.072 877.482 25623.68
    Greece | 178 3788.362 3989.601 848.2079 15826.62
    -------------+---------------------------------------------------------
    Portugal | 152 4008.019 4300.713 882.9161 14631.42
    Spain | 162 4629.319 4782.285 916.1738 17848.94
    Australia | 191 7248.124 6276.637 517.9641 25584.49
    NewZealand | 143 8252.141 4702.244 1144.444 19268.64
    Canada | 146 8604.375 7133.655 904.4118 25300.08
    -------------+---------------------------------------------------------
    UnitedStates | 211 8144.312 8381.661 1285.625 31654.93
    Albania | 67 2193.788 1087.947 446.1028 5374.991
    Bulgaria | 89 3879.027 2359.683 840.3462 9278.931
    Czechoslov~a | 88 5866.269 3132.538 849.027 13241.31
    Hungary | 90 4541.373 2139.806 1091.6 8825.765
    -------------+---------------------------------------------------------
    Poland | 76 4669.423 2368.137 945.9828 10762.46
    Romania | 80 2687.426 1211.344 816.3748 5040.325
    Yugoslavia | 89 3356.254 2098.784 551.4588 6982.198
    Bosnia | 56 3086.084 1232.635 915.967 5348.539
    Croatia | 59 6522.398 2536.402 1709.432 10724.56
    -------------+---------------------------------------------------------
    Macedonia | 59 4611.18 1574.115 1429.706 6720.768
    Slovenia | 59 10095.58 4462.771 2347.464 18745.07
    SerbiaandM~o | 57 3678.382 1660.701 984.8523 7408.191
    Serbia | 57 4669.438 1815.457 1234.422 7421.082
    Kosovo | 39 1310.408 408.5963 587.5695 1901.206
    -------------+---------------------------------------------------------
    AJ | 64 4254.666 1795.323 1211.047 6982.198
    CzechRepub~c | 21 9982.98 1899.848 7814.293 13350.31
    Slovakia | 21 8970.738 2240.168 6158.227 13037.09
    Armenia | 22 6206.202 2761.046 2971.061 11647.34
    Azerbaijan | 22 4034.338 2252.338 1808.796 8840.593
    -------------+---------------------------------------------------------
    Belarus | 22 7780.389 2754.988 4684.559 13658.55
    Estonia | 22 13563.74 4724.866 8050.377 22108.65
    Georgia | 22 4273.259 1558.697 2222.307 7615.813
    Kazakhstan | 22 7202.177 2185.134 4686.863 11258.17
    Kyrgyzstan | 22 2538.99 545.844 1772.363 3726.796
    -------------+---------------------------------------------------------
    Latvia | 22 9080.489 2925.066 5412.825 14723.64
    Lithuania | 22 8133.982 2408.801 4884.016 12666.73
    Moldova | 22 3345.404 1110.094 2176.248 6170.232
    Russia | 22 6441.788 1483.212 4461.81 9038.486
    Tajikistan | 22 1506.507 804.2495 824.5849 4094.946
    -------------+---------------------------------------------------------
    Turkmenistan | 22 2925.809 829.973 1894.528 4825.646
    Ukraine | 22 3910.562 1059.511 2545.504 6026.7
    Uzbekistan | 22 4016.05 865.7668 3077.692 6027.07
    USSR | 121 3316.571 2275.075 526.1814 7877.625
    Argentina | 141 4936.406 2306.313 930.6555 10256.28
    -------------+---------------------------------------------------------
    Brazil | 145 2259.989 1875.43 621.1956 6879.055
    Chile | 145 4182.821 2941.766 604.5472 13883.18
    Colombia | 145 2329.126 1803.749 491.6338 7062.505
    Mexico | 118 3523.938 2203.927 573.3281 7977.507
    Peru | 141 2233.2 1403.425 521.1989 5773.595
    -------------+---------------------------------------------------------
    Uruguay | 145 4331.622 2152.912 1085.806 11526.33
    Venezuela | 145 4944.625 3992.478 374.7736 11250.88
    Bolivia | 66 2209.938 398.347 1575.185 3064.28
    CostaRica | 91 3571.45 1871.497 1421.154 7996.778
    Cuba | 90 2069.54 662.7629 503.2854 3763.655
    -------------+---------------------------------------------------------
    DominicanRep | 61 2410.834 1130.335 1026.788 5379.087
    Ecuador | 114 2205.036 1435.598 411.241 5049.554
    ElSalvador | 89 1841.903 649.4586 823.1293 2940.419
    Guatemala | 91 2756.547 938.4791 1272.386 4491.118
    Haiti | 64 962.8466 157.6352 672.4241 1267.5
    -------------+---------------------------------------------------------
    Honduras | 89 1579.11 338.1812 1005.645 2323.11
    Jamaica | 77 2738.766 1182.269 530.4861 4130.375
    Nicaragua | 89 1824.198 590.6463 997.3333 3349.255
    Panama | 64 4060.473 1443.701 1851.326 6674.924
    Paraguay | 70 2372.01 723.8092 1495.171 3494.358
    -------------+---------------------------------------------------------
    PuertoRico | 59 8212.484 4181.74 2143.823 15313.92
    Trinidadan~o | 59 9545.259 3975.915 3674.051 21314.38
    China | 77 1709.894 1810.687 448.0217 8031.945
    India | 128 941.0461 565.4455 529.4956 3371.601
    Indonesia | 189 1137.035 945.1234 453.8728 4722.037
    -------------+---------------------------------------------------------
    Japan | 143 6326.298 7275.752 641.0652 22410.07
    Philippines | 106 1624.046 604.1 584.0993 3023.657
    KoreaSouth | 93 4852.852 6075.353 335.0285 21700.86
    Thailand | 67 3356.159 2691.229 569.9893 9372.209
    Taiwan | 103 5056.78 6269.802 606.4242 23291.91
    -------------+---------------------------------------------------------
    Bangladesh | 61 673.7865 196.0482 497.315 1276.231
    Burma | 73 962.0674 693.8224 395.6864 3708.939
    HongKong | 64 11972.53 8963.631 615 30724.62
    Malaysia | 98 3161.442 2689.593 602.7875 10094.49
    Nepal | 62 731.0641 182.8662 396.9774 1133.881
    -------------+---------------------------------------------------------
    Pakistan | 61 1297.033 577.9994 595.5755 2493.543
    Singapore | 102 7154.583 7807.501 863.7562 29037.56
    SriLanka | 143 1589.546 954.849 549.8763 5360.163
    Afghanistan | 59 685.2144 97.91586 426.0661 877.664
    Cambodia | 61 975.7587 504.7269 481.9768 2449.889
    -------------+---------------------------------------------------------
    Laos | 59 908.5181 259.9013 612.9426 1669.179
    Mongolia | 59 907.7634 272.4646 435.422 1404.823
    KoreaNorth | 62 1757.74 846.6817 335.0285 2841.079
    Vietnam | 64 1166.088 671.2854 505.4141 3216.812
    Bahrain | 61 3947.359 871.117 2104.454 5068.117
    -------------+---------------------------------------------------------
    Iran | 64 3733.608 1565.637 587.9573 6691.352
    Iraq | 64 2640.381 1474.274 588.2891 6755.605
    Israel | 61 10656.83 4940.523 2817.303 19170.78
    Jordan | 64 3403.072 1276.754 589.8618 5646.672
    Kuwait | 61 17906.74 8974.207 6120.886 33199.9
    -------------+---------------------------------------------------------
    Lebanon | 62 2917.116 722.5231 656.6265 4452.735
    Oman | 61 4631.291 2869.676 623.0946 9819.037
    Qatar | 61 21308.71 11692.44 6885.082 42916.24
    SaudiArabia | 61 8044.085 3205.253 2230.766 13420.94
    Syria | 64 5154.363 1960.669 658.19 7951.963
    -------------+---------------------------------------------------------
    Turkey | 91 3365.399 2144.43 712.1136 8224.861
    Yemen | 61 1941.023 782.8189 911.2311 3164.75
    Algeria | 64 2452.089 772.7062 430.2715 3512.581
    Angola | 61 1117.727 362.2823 565.923 1789.471
    Benin | 59 1117.126 132.9724 931.5844 1393.839
    -------------+---------------------------------------------------------
    Botswana | 59 2011.51 1539.299 348.5067 4768.838
    BurkinaFaso | 61 786.6547 188.225 474.3127 1234.09
    Burundi | 59 524.0524 104.9701 360.1425 722.5801
    Cameroon | 61 1052.419 233.2659 670.9511 1682.932
    CapeVerde | 59 1003.471 614.9647 448.6614 2735.376
    -------------+---------------------------------------------------------
    CentralAfr~c | 59 726.7742 140.2939 505.9934 942.8808
    Chad | 59 502.3315 91.6468 340.749 757.5002
    Comoros | 59 709.0519 151.4786 549.3492 1154.164
    CongoBrazz~e | 59 1936.391 467.2414 1198.207 3008.132
    CotedIvoire | 61 1468.281 298.781 1040.878 2040.988
    -------------+---------------------------------------------------------
    Djibouti | 59 1580.793 312.3075 1091.894 2185.019
    Egypt | 64 2006.329 1027.945 474.9642 4266.958
    Equatorial~a | 59 3556.444 5341.086 539.7625 22048.51
    Eritrea | 61 565.5025 111.5667 390.0746 934.7207
    Gabon | 59 5139.813 1757.591 3108.377 12339.93
    -------------+---------------------------------------------------------
    Gambia | 59 877.1726 143.1129 607.3647 1190.642
    Ghana | 63 1267.693 240.8424 438.8854 1922.391
    Guinea | 59 495.0182 90.55396 303.321 627.6444
    GuineaBissau | 59 694.9889 178.0814 289.1527 925.0669
    Kenya | 61 935.8598 151.0432 632.7559 1140.748
    -------------+---------------------------------------------------------
    Lesotho | 59 942.8107 448.943 355.366 1951.903
    Liberia | 59 1142.782 200.8717 696.7751 1530.391
    Libya | 59 3798.214 2311.376 857.1681 9114.819
    Madagascar | 61 917.548 198.6315 608.5276 1245.748
    Malawi | 61 518.8907 106.2733 324.0556 728.2377
    -------------+---------------------------------------------------------
    Mali | 61 696.1173 167.4849 456.8752 1058.509
    Mauritania | 59 915.5028 220.3255 463.9177 1321.714
    Mauritius | 59 5954.033 3556.311 2489.759 14529.24
    Morocco | 64 2133.56 813.9413 429.8996 4026.888
    Mozambique | 61 1431.388 392.2113 920.3522 2613.027
    -------------+---------------------------------------------------------
    Namibia | 59 3303.475 574.238 2159.705 4570.853
    Niger | 61 648.617 148.3369 439.1941 934.908
    Nigeria | 61 1098.236 274.7344 699.1183 1876.107
    Rwanda | 59 752.6854 153.1416 455.5972 1057.366
    SaoTomeand~e | 59 1269.194 277.9555 763.777 2009.121
    -------------+---------------------------------------------------------
    Senegal | 61 1359.581 96.76895 1186.46 1510.627
    Seychelles | 59 4002.383 1597.875 1912.258 7100.837
    SierraLeone | 59 862.2604 218.9134 403.5308 1161.545
    Somalia | 59 1084.611 156.1217 835.3474 1420.229
    SouthAfrica | 175 2102.162 1360.11 562.9661 5080.122
    -------------+---------------------------------------------------------
    Sudan | 61 1245.763 697.0465 743.9243 3132.299
    Swaziland | 59 2033.006 738.3578 721.0891 3150.209
    Tanzania | 61 552.8957 81.50587 423.7137 803.8524
    Togo | 59 803.5902 191.4656 573.994 1177.91
    Tunisia | 64 2842.096 1545.037 429.7143 6374.395
    -------------+---------------------------------------------------------
    Uganda | 61 738.9966 145.1342 538.2187 1158.379
    CongoKinsh~a | 61 548.3171 215.1734 203.4133 823.4555
    Zambia | 61 830.0398 163.0972 598.3341 1147.403
    Zimbabwe | 61 1116.56 249.4643 660.7354 1431.868
    FI | 0
    -------------+---------------------------------------------------------
    FJ | 0
    FK | 0
    FL | 0
    FM | 0


  • #2
    I was thinking of using:

    foreach x in Austria {
    gen log`x'=log(`x')
    }

    But it only works for Austria.

    Comment


    • #3
      No loops needed. You need to get this into proper panel-data format with one observation per country per year.

      Code:
      rename (Austria-Zimbabwe) gdpc=
      reshape long gdpc, i(year) j(country) string
      gen log_gdpc = log(gdpc)

      Comment


      • #4
        Hi Clyde! I ran it, but Stata shows this in red:
        variable id does not uniquely identify the observations
        Your data are currently wide. You are performing a reshape long. You specified i(year) and j(country). In the current wide
        form, variable year should uniquely identify the observations. Remember this picture:

        long wide
        +---------------+ +------------------+
        | i j a b | | i a1 a2 b1 b2 |
        |---------------| <--- reshape ---> |------------------|
        | 1 1 1 2 | | 1 1 3 2 4 |
        | 1 2 3 4 | | 2 5 7 6 8 |
        | 2 1 5 6 | +------------------+
        | 2 2 7 8 |
        +---------------+
        Type reshape error for a list of the problem observations.

        Comment


        • #5
          It did change de name to gdpc_countryname, but the numbers are the same. No in log

          Comment


          • #6
            Please show your code, we can't help you if you don't give code.

            Comment


            • #7
              My code is:
              rename (Austria-Zimbabwe) gdpc=
              reshape long gdpc, i(year) j(country) string
              gen log_gdpc = log(gdpc)

              The error says:
              gdpc ambiguous abbreviation

              Comment


              • #8
                This doesn't make sense. This works for me

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input int year double(Austria Belgium)
                1800 . .
                1801 . .
                1802 . .
                1803 . .
                1804 . .
                1805 . .
                1806 . .
                1807 . .
                1808 . .
                1809 . .
                1810 . .
                1811 . .
                1812 . 1478.9104126809905
                1813 . .
                1814 . .
                1815 . .
                1816 . .
                1817 . .
                1818 . .
                1819 . .
                1820 1218.1656277827249 .
                1821 . .
                1822 . .
                1823 . .
                1824 . .
                1825 . .
                1826 . .
                1827 . .
                1828 . .
                1829 . .
                1830 1398.5302430751838 .
                1831 . .
                1832 . .
                1833 . .
                1834 . .
                1835 . .
                1836 . .
                1837 . .
                1838 . .
                1839 . .
                1840 1514.5317545748117 .
                1841 . .
                1842 . .
                1843 . .
                1844 . .
                1845 . .
                1846 . 1693.9013035381752
                1847 . 1761.5970459266098
                1848 . 1753.6032944406315
                1849 . 1790.3811252268601
                1850 1650.379746835443 1846.7071251966734
                1851 . 1885.637703819522
                1852 . 1923.6573457612071
                1853 . 1961.6232906925452
                1854 . 2069.6909927679158
                1855 . 2070.775261324042
                1856 . 2169.6602466998484
                1857 . 2211.352397333907
                1858 . 2211.538461538462
                1859 . 2197.4522292993634
                1860 1777.567886658796 2292.616033755274
                1861 . 2310.2220360284878
                1862 . 2353.9197338323975
                1863 . 2403.7985136251036
                1864 . 2466.079114572658
                1865 . 2448.0162767039674
                1866 . 2502.626262626263
                1867 . 2497.392699558765
                1868 . 2569.1817638861235
                1869 . 2662.557168423146
                1870 1862.5904867256638 2691.5227629513347
                1871 1979.0957913195969 2682.4648627603656
                1872 1976.3223284100782 2823.6469679412903
                1873 1913.0281962978909 2820.0584403142366
                1874 1980.929180887372 2889.7719064816574
                1875 1973.2553911205073 2860.7850273430136
                1876 2000.116722548198 2874.6164639850326
                1877 2050.4404984423672 2884.4840563589173
                1878 2100.3057442865966 2942.257442116869
                1879 2067.93876301286 2944.919883466861
                1880 2078.837887067395 3064.8213318895505
                1881 2145.166700100301 3069.676061362826
                1882 2139.9620278330017 3136.056760091662
                1883 2208.783842364532 3144.5341463414634
                1884 2248.4751025190394 3136.10487342862
                1885 2215.2706155632986 3137.8131381892445
                1886 2267.7099002302375 3153.2723433012334
                1887 2404.4300932090546 3249.8914793693393
                1888 2379.1510465774095 3247.0852338937902
                1889 2337.2069932685117 3378.5000826309697
                1890 2443.3559510567297 3427.8480971128606
                1891 2506.147998531032 3395.280499675536
                1892 2535.1280886627906 3441.8329321136257
                1893 2525.1031817364733 3455.4811111111117
                1894 2644.598719316969 3468.289010989011
                1895 2687.7818661971833 3511.5006988662844
                1896 2701.0125457396757 3551.4953803510934
                1897 2730.1526388409798 3586.4266951740988
                1898 2855.287397540984 3614.7919442761963
                1899 2886.2769230769236 3656.1529570699
                
                end
                cls
                rename (Austria-Belgium) gdpc=
                reshape long gdpc, i(year) j(country) string
                gen log_gdpc = log(gdpc)
                Note that you already can get GDP data from here without needing to reshape anything.
                Code:
                u "https://www.rug.nl/ggdc/historicaldevelopment/maddison/data/mpd2020.dta", clear
                
                
                g lngdp = ln(gdp)
                
                br
                // boom boom, GDP data logged.

                Comment


                • #9
                  Yes, but unfortunately i must use reshape comand

                  Comment


                  • #10
                    I can also use the comand renvars, but I do not know how

                    Comment


                    • #11
                      No, you literally don't need to reshape here, that's my point I'm making to you. Run the code I just gave to you at the bottom, it literally has GDP per capita data from year 1 to 2018. No need to use reshape, I promise you.


                      Anyways, if you're still having issues, what I now need you to do is give your full data example (not just two countries, give as many as Stata will allow), and then run your exact code and share that in the next post (properly formatted, in the code delimiters, using the number sign in the toolbar). The point I'm trying to make here is that for us to help you, you have to give us a minimal example that reproduces your error. After using your dataset and code, I should be able to get the error "ambiguous abbreviation", but I don't, the example I gave above ran smoothly. So, let's make this more reproducible, yes?

                      Comment


                      • #12
                        Here's the best I can figure to what you've done wrong. First, you got an error message saying that the data were not identified by the variables specified in your -reshape- command's -i()- and -j()- options. You spoke of that in #4. The -reshape- did not actually execute. What you should have done then is recognized that your data are not as expected and stopped. But instead you tried to apply the subsequent -gen log_gdpc ...- command anyway. But at that point, because -reshape- didn't actually apply, there are still many variables named gdpc* and Stata can't know which one you want to apply log transformation to, leading to the error message you report in #7.

                        The root cause is that the data are not as expected. In order for -reshape- to work, there must be only one observation (row) for each value of the variable year. In your example data, that condition is met, and the code (modified to apply only to Austria and Belgium since the other countries aren't in the example) runs without errors. But apparently in your real data there is some year (perhaps several) for which you have more than one observation. This means your data are unsuited to the task you are trying to accomplish. So the first thing you need to do now is find out which years have duplicate observations, and then you have to figure out how that happened and fix it.

                        Code:
                        duplicates tag year, gen(flag)
                        browse if flag
                        will show them to you.

                        Once you see them, you need to figure out how the data set came to have these excess observations and you have to fix it. If you, or somebody in your shop, created this data set, you/that person should review the data management that led to its creation and fix whatever created this situation. While you are doing that, you should scrutinize the entire data management process, because where one error is found, others often lurk. You should debug your data as early in the project as possible. If you cannot review and modify how the data set is created (e.g. it was provided by an external party) then you have to just change it. There are several possibilities in a case like this:
                        1. The offending "years" are actually observations with missing values. There is no actual data in them. (Blank observations sometimes end up in data sets when they are created by importing from a spreadsheet or other non-Stata data source.) In that case you can just -drop- those observations.
                        2. The observations that duplicate any given year are actually complete duplicates on all variables: they are identical. In that case, you can pick one to keep and drop the others.
                        3. The observations that duplicate a year contain different values for the other variables. Then you have a serious problem on your hands: your data are internally inconsistent, with different observations telling you contradictory things about the same country in the same year. In that case you will have to do some research to try to find out which of the observations, if any, is correct and retain only that one. If none are correct, you need to drop them all, and, if possible, add to the data set a correct observation for that year obtained from some other source.
                        Added: Crossed with #8-#11.
                        Jared Greathouse He does need to use -reshape-. Of course, he can log-transform all the individual country variables, but because he is going to be combining this data set with other long-layout panel data for his project (which is not disclosed in this thread, but can be inferred from a separate thread he began yesterday) at some point he needs to -reshape- the data, and now is as good a time as any. And, even if he never needed to -reshape- the data, having discovered that his data set contains duplicate year observations he needs to fix that problem come what may. Otherwise all his subsequent analyses will be based on incorrect data (unless the duplicate observations contain only missing values.)
                        Last edited by Clyde Schechter; 03 Sep 2022, 17:25.

                        Comment


                        • #13
                          Thank you as always Clyde. Is there a simpler way to do this without reshape?
                          I was thinking in:
                          Code:
                          foreach x in (Austria-Zimbabwe) {
                          replace `x'=log(`x')
                          }

                          But stata says "too many variables specified"

                          Comment


                          • #14
                            Originally posted by Andres Henriquez View Post
                            Thank you as always Clyde. Is there a simpler way to do this without reshape?
                            I was thinking in:
                            Code:
                            foreach x in (Austria-Zimbabwe) {
                            replace `x'=log(`x')
                            }

                            But stata says "too many variables specified"
                            There are two main points:

                            1) The command does not work because a variable list "Austria-Zimbabew" was provided but the foreach command is not telling Stata that there is a varlist coming; it needs to be:

                            Code:
                            foreach x of varlist Austria-Zimbabwe
                            2) Please avoid overwriting variable in this manner. If you accidentally run this code again without re-open a new file you can risk log-transforming the variables more than once. When creating a transformed variable, make a new copy:

                            Code:
                            foreach x of varlist Austria-Zimbabwe{
                                generate log_`x' = log(`x')
                            }
                            Also, I'm late to this thread but my guess that your reshape in message #4 didn't work may be related to the data you imported. Sometimes, when important data from sources like Excel, there could be some number of empty rows attached at the end. To Stata, if multiple cases have years as missing (.), they are considered duplicates. So, you may try to remove those empty cases then run Clyde's suggested reshape command:

                            Code:
                            drop if year == .
                            If the duplicate problem persists, use duplicates reports to check if there any duplicates:

                            Code:
                            duplicates report year
                            There are a set of useful commands under duplicates which can help tag or even remove duplicates. For more, please check the technical document with help duplicates.

                            Good luck.

                            Comment


                            • #15
                              Code:
                              foreach x of varlist Austria-Zimbabwe {
                                  gen log_gdp_`x' = log(`x')
                              }
                              But heed what I said in #12. Sooner or later, you are going to have to -reshape- this data, and putting it off only makes the work longer, more complicated, and more error prone. The sooner you go to long layout, the better everything will be. So I advise you against doing this.

                              You need to develop a different attitude about programming. Error messages are not obstacles to be pushed through or worked around. They are your friends. They are pointing out that something is wrong, and you ignore them at your peril. If -reshape- is throwing messages at you it's because your data are wrong. You cannot get right results from wrong data. Working around it, as you propose here, doesn't fix that problem. It just postpones the reckoning. And if you are really unlucky, the reckoning will be postponed so long that it will be triggered by somebody else seeing that things are wrong when you are presenting your results and calling you out, or, worse still, relying to their harm, on your wrong results.

                              Added: Crossed with #13, which offers similar advice and reinforces other points made earlier in the thread.
                              Last edited by Clyde Schechter; 03 Sep 2022, 18:40.

                              Comment

                              Working...
                              X