Announcement

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

  • Loop over variables

    input double(month202001 month202002 month202003 month202004 month202005 month202101 month202102 month202103 month202104 month202105 month202201 month202202 month202203 month202204 month202205)
    276.16 275.63 276.83 278.39 273.53 290.03 290.3 291.96 293.88 287.92 307.02 308.21 310.12 312.38 309.28
    300.11 299.13 300.74 303.39 294.08 315.81 315.35 317.32 320.28 308.41 333.51 334.95 337.43 340.25 334.02
    245.46 245.49 246.17 246.34 247.17 256.97 258.18 259.44 260.02 261.65 273.05 273.93 275.11 276.64 277.57
    290.95 291 291.44 291.49 291.72 301.15 302.12 303.73 304.23 305.17 321.57 323.49 325.31 328.18 329.4
    224.99 225.06 225.93 226.04 226.3 229.98 229.35 229.5 229.44 225.9 233.53 233.65 234.17 234.65 235.01
    285.35 284.83 285.51 285.79 285.8 297.45 299.53 302.63 305 306.46 320.51 322.05 323.51 325.93 328.4
    232.8 232.88 233.28 233.7 239.36 249.01 250.79 251.13 251.5 251.53 253.44 253.57 253.91 254.59 255.05
    248.96 249.14 249.97 250.22 252.04 269.58 272.6 274.68 276.02 294.86 315.7 317.11 317.88 319.82 320.76
    190.66 190.69 190.92 190.96 190.96 194.13 195.12 195.89 195.95 195.99 203.16 203.76 204.95 206.59 207.47
    262 262.23 263.15 263.41 263.66 291.53 296.15 299.06 299.67 299.99 312.88 314.16 317.85 319.99 321.15
    end
    [/CODE]


    Above is the extract of my data, each variable is consumer price index (cpi) of each year and each month for ex month202001 is cpi of year2020 and month01, I want to create for year 2021 and 2022 the inflation rates for each month using the formula inf_202101=month202001-month202101/month202101, using foreach loop as I have many years in actual data set.

  • #2
    maybe,
    Code:
    foreach v of var month202101-month202205 {
    local a = real(substr("`v'",6,6))
    local b = `a' - 100
    gen inf`v'= (month`b' - month`a') / month`a'
    }

    Comment


    • #3
      Thank you

      Comment


      • #4
        Oyvind Snilsberg gave you what you asked for, but working with such a wide layout as you have is at best awkward and at worst a small nightmare. You will be much better off for most Stata purposes with a long layout, as follows:

        Code:
        clear 
        input double(month202001 month202002 month202003 month202004 month202005 month202101 month202102 month202103 month202104 month202105 month202201 month202202 month202203 month202204 month202205)
        276.16 275.63 276.83 278.39 273.53 290.03 290.3 291.96 293.88 287.92 307.02 308.21 310.12 312.38 309.28
        300.11 299.13 300.74 303.39 294.08 315.81 315.35 317.32 320.28 308.41 333.51 334.95 337.43 340.25 334.02
        245.46 245.49 246.17 246.34 247.17 256.97 258.18 259.44 260.02 261.65 273.05 273.93 275.11 276.64 277.57
        290.95 291 291.44 291.49 291.72 301.15 302.12 303.73 304.23 305.17 321.57 323.49 325.31 328.18 329.4
        224.99 225.06 225.93 226.04 226.3 229.98 229.35 229.5 229.44 225.9 233.53 233.65 234.17 234.65 235.01
        285.35 284.83 285.51 285.79 285.8 297.45 299.53 302.63 305 306.46 320.51 322.05 323.51 325.93 328.4
        232.8 232.88 233.28 233.7 239.36 249.01 250.79 251.13 251.5 251.53 253.44 253.57 253.91 254.59 255.05
        248.96 249.14 249.97 250.22 252.04 269.58 272.6 274.68 276.02 294.86 315.7 317.11 317.88 319.82 320.76
        190.66 190.69 190.92 190.96 190.96 194.13 195.12 195.89 195.95 195.99 203.16 203.76 204.95 206.59 207.47
        262 262.23 263.15 263.41 263.66 291.53 296.15 299.06 299.67 299.99 312.88 314.16 317.85 319.99 321.15
        end
        
        gen long id = _n
        reshape long month, i(id) j(Mdate)
        
        gen mdate = ym(floor(Mdate/100), mod(Mdate, 100))
        format mdate %tm
        drop Mdate 
        rename month whatever 
        l if id == 1 
        
             +------------------------+
             | id   whatever    mdate |
             |------------------------|
          1. |  1     276.16   2020m1 |
          2. |  1     275.63   2020m2 |
          3. |  1     276.83   2020m3 |
          4. |  1     278.39   2020m4 |
          5. |  1     273.53   2020m5 |
             |------------------------|
          6. |  1     290.03   2021m1 |
          7. |  1      290.3   2021m2 |
          8. |  1     291.96   2021m3 |
          9. |  1     293.88   2021m4 |
         10. |  1     287.92   2021m5 |
             |------------------------|
         11. |  1     307.02   2022m1 |
         12. |  1     308.21   2022m2 |
         13. |  1     310.12   2022m3 |
         14. |  1     312.38   2022m4 |
         15. |  1     309.28   2022m5 |
             +------------------------+
        You should already have an identifier, and if so use it rather than create a new one.

        Comment


        • #5
          Thank you for your input, I see what you mean.

          Comment

          Working...
          X