Announcement

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

  • Creating a new index with different base years and looping

    Hello,
    I wrote a similar post few hours ago, but for some reason i'm not authorised to edit the previous post, so here I am posting the edited version, sorry for the incovenience.
    I'm pretty new on Stata (version 17) and I would appreciate your help on the following problem, on which I've been stuck for several hours.

    I have a dataset on yearly british output (agricolture, coal, mining, total industry, services etc...) from 1700 to 1870, as you can see below. All variables (except for 'year', obviously) are indexes and the base year is 1700 (1700=100). For each variable (except 'year'), I would like to create 3 new indexes, with a different base year, in particular 1725=100, 1789=100 and 1870=100.

    Here there is a sample of my data, considering some variables:
    Code:
    dataex year tot_output_agr_1700 Tin_ind_1700 Iron_ind_1700 Coal_ind_1700 WoolTextiles_ind_1700 Leather_ind_1700 Foodstuffs_ind_1700 Construction_ind_1700, count (171)
    
    
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year double(tot_output_agr_1700 Tin_ind_1700 Iron_ind_1700 Coal_ind_1700 WoolTextiles_ind_1700 Leather_ind_1700 Foodstuffs_ind_1700 Construction_ind_1700)
    1700     100     100       100      100     100     100     100      100
    1701 111.063  95.313    98.977   118.75 102.752 103.256 101.898   81.513
    1702 117.736  78.125    99.348    68.75   96.28 100.508 103.707   79.832
    1703 104.666   112.5    98.742     87.5  92.374  97.756 112.829   94.958
    1704 131.248 103.125    98.401   106.25  92.552   96.95 110.776  109.244
    1705 127.027  98.438    97.415      100  97.666 101.038  117.48  101.261
    1706  98.721 103.125    97.862     87.5  94.419  99.744 106.877   93.277
    1707 115.155 101.563     98.62    81.25 100.532  104.26 111.743  117.647
    1708  91.182 101.563    98.734   106.25 106.856 107.075 109.024  105.042
    1709  77.499     100    98.385    112.5   49.05  67.988 103.846   84.034
    1710   72.16 151.563   100.713    93.75  54.224  70.881  98.891   79.832
    1711  93.152     100   100.781   106.25  58.128  71.873 102.074   55.462
    1712 100.319     100   102.213      100  61.441  73.633 102.074   55.462
    1713  77.248   93.75   102.911      100  53.384  68.573  94.507   85.714
    1714  95.813  78.125   104.101      125  58.901  74.029  99.578  101.681
    1715  84.364  82.813   105.746    112.5  65.578   79.81  99.698  115.966
    1716  85.427      75   115.297   118.75  54.594  75.892 106.317   81.513
    1717  95.932 115.625   125.152   131.25  58.463  81.434 110.584   96.639
    1718 115.785 114.063   138.758   118.75   59.87  83.575 112.323  111.765
    1719  95.744 109.375   135.893      125  67.094  87.622 115.895  110.084
    1720 132.443 103.125   134.513      125  76.281  95.004 115.973  140.336
    1721 128.661  79.688   128.987    137.5  76.277  94.117  116.31  111.765
    1722 118.288  96.875   122.931    137.5  78.174  102.07 117.633  121.008
    1723 109.716  95.313   122.976    137.5  71.687 100.681 117.539  106.723
    1724 114.304 110.938   122.953   131.25  87.857  98.945 112.085  142.017
    1725   117.2 115.625   122.847    137.5  88.442 101.375 111.441  142.857
    1726 100.414  106.25   120.672      150  70.362 103.459  111.08  117.647
    1727 104.657 110.938    120.52   143.75  70.907 102.417  113.37  115.966
    1728 121.705 101.563   118.367   131.25  69.946  98.945 106.089  129.412
    1729 117.667 110.938   114.835   143.75  51.648  95.126 104.216    99.16
    1730 113.608 107.813   112.098   131.25  69.189  98.598 111.328  111.765
    1731 110.484  96.875   111.302    137.5  69.224  96.515 109.055  103.361
    1732 131.295 129.688   107.914   131.25  47.129  97.557 112.164  134.454
    1733 130.545 114.063    107.99   143.75  58.869  97.209 117.319  123.529
    1734 125.576 128.125   103.244   131.25  60.087  98.598 109.675   126.05
    1735 115.534 121.875   102.532      150  60.904 102.417  110.06  118.487
    1736 146.197 107.813    96.869      150  61.548 108.666 108.278   124.37
    1737 142.602 117.188    98.575    137.5  53.313 108.666  110.94  121.008
    1738 130.791   93.75   103.578   143.75  53.698 105.194 109.369  113.445
    1739 130.633     125   112.401   131.25  35.347 106.583 113.672   84.874
    1740 128.323  118.75   114.198   168.75  35.795  97.557 104.854    99.16
    1741 131.598 107.813   115.873   131.25  37.584  98.945 105.723  121.849
    1742 141.842     125   122.279    137.5  37.584  96.862  109.45  131.933
    1743 128.124  131.25   121.983   143.75  38.032  92.696  112.82   125.21
    1744 117.839 129.688   124.674    137.5  40.269  91.307 115.209   100.84
    1745 121.736 120.313   128.457    137.5  39.821  89.224 106.402  118.487
    1746 128.983 132.813   131.087   143.75  42.059  92.696 105.637  130.252
    1747 149.494 128.125   136.075    137.5  44.296  96.862 111.502  105.042
    1748 128.015 139.063   139.683   131.25  44.296  94.779 114.701  131.092
    1749 128.963  79.688   142.715      150  44.296   99.64 111.779  113.445
    1750 128.047     200   147.574    137.5   46.98 102.417 118.166  143.697
    1751 123.972 157.813   150.879  156.941  46.085  99.987 115.204  127.731
    1752 137.624 179.688     152.1  151.326  45.191 105.542 114.659  141.176
    1753 140.156 179.688   151.903  151.989  44.296   104.5 117.567  139.496
    1754 136.297 192.188   153.335  159.012  45.191 101.028 116.882  138.655
    1755  136.53 192.188   153.206  146.927  44.743 101.375 115.037  160.504
    1756 133.477 192.188   152.994  167.735  40.269   104.5 104.236  152.101
    1757 156.443 192.188   155.367  156.351  45.191 102.417 102.409  131.933
    1758 162.924 192.188   155.867  138.135  43.848   104.5 112.657  131.092
    1759 157.392 185.938   156.178  172.669  40.716 107.972  112.93   149.58
    1760 164.727 192.188    158.71  160.904  41.164 104.847 123.224  123.529
    1761 172.378 165.625   158.338  159.483  42.059 108.666 121.078  153.782
    1762 161.681 179.688   159.066  164.648  41.164 112.485 114.144  132.773
    1763 163.445 192.188   160.658  182.748  39.374 110.055  110.01  179.832
    1764 161.329 185.938   164.107   181.09  42.059  120.47 114.977  163.866
    1765 153.503 192.188     166.7  179.433  41.164 114.221 110.049  205.882
    1766 132.912 214.063   165.676  194.959  45.638 109.708 105.701  214.522
    1767 159.154     200   166.609   184.49  53.692 107.625 104.336  171.292
    1768 165.813 185.938   166.631  193.698  48.323 104.847 110.573  185.821
    1769  181.86     200   167.314  203.087  51.902 107.277 109.442  201.772
    1770  164.75  206.25   167.533  198.936  51.455 107.972 109.849  201.797
    1771 175.416     200   166.389  220.911  51.902 111.096 109.784  253.213
    1772 150.736 220.313   160.681  236.201  53.244 114.221 113.829  263.738
    1773 175.796     200   158.899  216.711  55.929 116.998 110.108  242.642
    1774 169.247 171.875   158.126  203.995  43.848 114.915 109.011  214.774
    1775 173.607 185.938   158.983  219.415  45.638 113.874 112.216   258.48
    1776 179.923 185.938   159.809  239.107  47.428 111.791 102.792   252.79
    1777 186.984 192.188   164.206   237.23   48.77  115.61 104.961  264.879
    1778 179.953 179.688   164.289  227.437  54.587 116.651   106.7  278.726
    1779 184.171 185.938   162.477  216.972  49.218 113.874 109.244  267.506
    1780 197.185  206.25   158.179   245.42  42.953 113.527 116.969  260.259
    1781 216.931 185.938     157.3  245.683  43.848 116.304 111.245    263.3
    1782 193.065 179.688   166.002  245.947  51.902 119.776 114.545  284.953
    1783 188.081 179.688   170.421  262.095  55.482 121.859  99.829  456.278
    1784 192.249 185.938   166.578  270.327  53.244 119.081 124.426   316.83
    1785 176.828     200   192.382  278.576  54.587 122.553 121.966  305.968
    1786 188.477 234.375   213.614  283.349  57.271 121.165 118.195  389.464
    1787 168.894 226.563   225.697  255.193  59.061 122.553  123.85   403.31
    1788 166.825 165.625   250.068  301.263  55.482 124.636 123.334  369.743
    1789 171.082 240.625   261.355  323.182  58.166 118.387 121.698  331.431
    1790 195.533 220.313   292.594  311.082  59.508 128.455 122.765   376.15
    1791 194.329 240.625   302.403  334.229  57.271 130.191  132.51  347.977
    1792  197.48  268.75   318.769  339.951  68.457 129.844 138.493  491.855
    1793 198.725 226.563   333.831  320.249  61.746 134.357 138.239  389.214
    1794 170.143 234.375   336.325  309.187  58.166 137.829 138.874  341.783
    1795 213.306 240.625   354.753  356.265  67.562 135.399 138.882  378.403
    1796 212.443 214.063   357.345  326.957   68.01  145.12 148.117  536.877
    1797  206.52 226.563   415.631  347.753   66.22 134.357 156.404  421.419
    1798 211.483     200   455.996  308.882  63.088 134.357 153.112  322.965
    1799 204.051     200    477.13  355.811  72.037 129.497 160.158   318.58
    1800 201.878 179.688   534.733  421.277  72.931 135.052 124.717  420.579
    1801 236.646 165.625   590.888  359.343   64.43 138.871 124.592  484.218
    1802 223.838 185.938   610.817  431.956  65.325  142.45 136.116  367.733
    1803 211.054  206.25   659.953  432.286  66.667  125.27 139.813  429.428
    1804 202.692 214.063   641.419  480.225  73.379 125.986 133.177  422.962
    1805 228.333 192.188   653.252  480.762  77.406 143.166 135.176  447.231
    1806 222.911  206.25   711.128  502.171  76.063 141.018 139.679  350.078
    1807  239.87 171.875   769.504  454.549  72.931 139.586 144.558  505.559
    1808  234.74 165.625    803.73  525.575  68.457 144.597 145.844  300.384
    1809 238.802 179.688   801.918  467.722  75.169  151.04 136.254   367.35
    1810 231.456 145.313   819.019  539.273  70.694 153.903 142.818  636.136
    1811 210.473 165.625   903.631  539.719   68.01 143.166 156.465  653.423
    1812 209.716 165.625   898.514  550.152  71.589 135.291 146.941  464.907
    1813 253.458 165.625   884.771   539.43   81.88 153.903 145.452  347.641
    1814 221.964 185.938   866.343  614.112  79.643 157.482 151.945  249.843
    1815 235.692  206.25   854.586  614.362  80.985 178.957 152.255    538.6
    1816 224.538 234.375    707.11      656  75.169  181.82   149.1  412.148
    1817 242.947 289.063   690.752  610.172   80.09 200.432 155.822  394.719
    1818 223.088 282.813   732.921  651.511  88.592 193.273 165.397  448.192
    1819 216.353 234.375   727.373  627.625  82.327 184.684  164.21  543.559
    1820 261.479  206.25   825.644  679.374  77.853 171.799 189.951  516.469
    1821 258.563 234.375   993.185  664.766  84.565  173.23 164.459  533.545
    1822 251.861 226.563   924.098  693.749  86.354 176.809 174.166   522.16
    1823 255.045 295.313  1140.214  778.819  87.697 193.989 161.392   554.88
    1824 262.919 351.563  1339.804  786.806  90.829 209.737 172.648  631.941
    1825  263.34 303.125  1401.903  806.147 106.489 206.874 193.367  711.421
    1826 241.535 323.438  1270.558  863.834  87.697 182.536 200.536  721.464
    1827 263.552 385.938  1666.927   797.98  97.093 198.284 186.545  605.955
    1828 235.256  343.75  1690.706  810.115   99.33 229.781  202.01  654.232
    1829 245.454 309.375  1665.767  886.579  94.856 234.076 200.593  743.878
    1830  253.77 309.375   1648.81  960.875 104.252 244.813 211.406  680.357
    1831 258.836 303.125  1460.241  970.544 102.462 224.054 231.502  804.058
    1832 279.805 303.125  1542.245 1035.425  99.777 199.716 214.153  872.127
    1833 261.038 282.813  1905.139 1045.323 107.831 231.928 222.961  838.176
    1834 266.426  206.25  1925.136 1054.764 113.648 256.982 226.714   802.29
    1835 308.181 295.313  2278.373 1146.483 108.279 240.518 233.129  990.339
    1836 293.711 282.813  2373.651 1203.675 126.623 252.687 238.871  914.365
    1837 303.659 329.688  2538.281  1314.07 114.543 241.234 232.522 1012.352
    1838 305.327 357.813  2758.141 1329.908 117.227 248.392 236.255  966.737
    1839 290.023 371.875  3049.788 1322.109 122.149 260.561 236.093   982.72
    1840 313.449 364.063  3408.586 1355.691 115.437 243.381 241.309  958.398
    1841 289.705   362.5  3587.986 1414.644 119.017 291.342 237.588  914.183
    1842 315.586   362.5   2556.44 1431.095 110.516  315.68 230.577  673.492
    1843 315.779 371.875  2690.989 1373.989   113.2  315.68 230.428  873.309
    1844 346.005     400  4798.931 1348.588 126.176 346.461 233.216    923.9
    1845 326.093 420.313  3677.685 1656.761 133.782 348.608 239.263 1338.085
    1846 319.086 434.375  4215.883 1575.548 127.071 292.773 246.149 1517.256
    1847 339.218 446.875  4664.381 1790.891 122.596 321.407 249.691 1246.733
    1848  319.21 460.938  4843.781 1849.814 128.413 302.795 242.648 1346.573
    1849 337.183  481.25   4933.48 1747.883 125.281 317.827 256.508 1134.807
    1850 309.692  468.75  5381.978 1912.749 121.254 297.784 273.316 1058.091
    1851 323.217 426.563  5875.326 2083.542 127.966 313.532 285.296 1066.358
    1852 344.759 434.375  6234.125 2072.634 137.809 282.752 249.584 1113.914
    1853 297.485     400  6772.323 2050.816 157.496 377.957 263.218 1341.043
    1854 370.039   412.5  7041.422 2454.435 137.809 309.238  295.71 1508.173
    1855 339.095 420.313   7400.22 2496.921 125.281 297.068 257.371 1459.811
    1856 341.147 426.563  8207.517 2581.895 133.335 308.522 277.255 1403.607
    1857 322.471 460.938  8207.517 2676.674 138.704 414.464 298.923 1487.763
    1858 344.257  481.25  7803.869 2624.382 144.968 305.658 301.415 1574.925
    1859 319.346 495.313  8611.165 2706.088 150.785 347.176 282.395 1671.103
    1860 281.602  468.75  8835.415 2872.767 157.496 339.302 285.828  1782.31
    1861 308.776 523.438  8431.766 2915.254 138.704 324.986 301.017 1610.992
    1862 300.162 592.188  8925.114  2977.35 158.838 340.018 342.155 2016.745
    1863   409.4 701.563 10315.459 3127.688 154.364 349.324  343.38 2109.918
    1864 408.524 709.375 11033.056 3238.808 182.105 383.684 335.119 2236.153
    1865 361.291 701.563 10943.356 3343.391 170.919 391.558 322.892 2335.337
    1866 354.659 698.438 10270.609 3428.365 202.239 442.381  323.57 2422.499
    1867 359.016  606.25 10808.807 3477.388 188.369 378.673 326.083 2515.672
    1868 386.469 646.875 11391.854 3536.216 199.555 423.054 372.223 2593.817
    1869 398.122  681.25   12333.7 3624.458  196.87 365.072 372.414 2662.945
    1870 383.662 715.625 13679.195 3774.796  214.32 445.245 368.308 2744.096
    end

    Now, I know how to create every single new index 'manually'; for example, to create 3 new indexes for agricoltural output based in 1725, 1789 and 1870, I would use, respectively, these commands:
    Code:
    generate tot_output_agr_1725 = tot_output_agr_1700/tot_output_agr_1700[26]*100
    generate tot_output_agr_1789 = tot_output_agr_1700/tot_output_agr_1700[90]*100
    generate tot_output_agr_1870 = tot_output_agr_1700/tot_output_agr_1700[171]*100
    where [26] [90] and [171] are the observation numbers for 1725, 1789 and 1870. Repeating these commands for all my variables would require a lot of typing... and i can't really figure out how to loop such a command.
    Is there any way to loop the creation of the new indexes?


    Can someone help me with this?


    Thank you.

    PS: sorry if my english is not perfect

  • #2
    I can't answer your question, but regarding editing, you can only edit a post in the first hour after you posted it. But it would have been correct to simply put the updated version into a new reply on the original topic. I have gone back to the original and added a reply with a link to this topic so that anyone following the original topic and waiting for updates will be directed to this topic.

    Comment


    • #3
      See https://www.statalist.org/forums/help#closure for an explanation of the scope for editing, which is in the FAQ Advice everyone is asked to read before posting.

      There are indeed better ways to change the base of indexes.Here is one:


      Code:
      foreach base in 1725 1789 1870 { 
           su tot_output_agr_1700 if year == `base', meanonly 
           generate tot_output_agr_`base'  = 100 * tot_output_agr_1700/`r(mean)' 
      }
      If this were panel data, the means (for several panels) would be better put into a variable.



      Comment


      • #4
        Nick Cox Thank you for the advice and for your help. Your command works perfectly.

        Comment

        Working...
        X