Announcement

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

  • Foreach varlist

    Hi,

    I have a simple problem, but I haven't been able to solve it yet. In my panel data, I want to make a homogeneous data set. There are a few variables that make indexes =100 with 2015, while others make indexes=100 with 2003. I would like to make all variable indexes=100 with 2015. In my following data, there is index=100 with 2003,
    Using the "foreach" code, trying to generate the data,

    "" foreach var of varlist inf {
    egen t2015 = total(cond(year==2015,`var',0))
    replace `var' = `var' * 400 / t2015
    drop t2015
    }"",
    However, it seems to me what it generated is not the right approach to this case.


    Thanks in advance for your advice.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id qdate) str103 industry double inf
    1 172 "10. (Food Products)"     94.51333333333332
    1 173 "10. (Food Products)"     98.79666666666667
    1 174 "10. (Food Products)"    101.90333333333335
    1 175 "10. (Food Products)"    104.78333333333335
    1 176 "10. (Food Products)"    108.48333333333333
    1 177 "10. (Food Products)"                112.86
    1 178 "10. (Food Products)"                117.29
    1 179 "10. (Food Products)"    119.81333333333333
    1 180 "10. (Food Products)"    121.92999999999999
    1 181 "10. (Food Products)"    122.86333333333333
    1 182 "10. (Food Products)"                122.25
    1 183 "10. (Food Products)"    121.33333333333333
    1 184 "10. (Food Products)"                121.73
    1 185 "10. (Food Products)"    120.49333333333334
    1 186 "10. (Food Products)"    121.85333333333335
    1 187 "10. (Food Products)"    124.68666666666667
    1 188 "10. (Food Products)"    128.27666666666667
    1 189 "10. (Food Products)"    131.27666666666664
    1 190 "10. (Food Products)"    136.23999999999998
    1 191 "10. (Food Products)"                143.15
    1 192 "10. (Food Products)"    147.94333333333333
    1 193 "10. (Food Products)"                156.71
    1 194 "10. (Food Products)"    157.68333333333334
    1 195 "10. (Food Products)"    156.44999999999996
    1 196 "10. (Food Products)"                   159
    1 197 "10. (Food Products)"    161.32999999999998
    1 198 "10. (Food Products)"    163.24333333333334
    1 199 "10. (Food Products)"                168.29
    1 200 "10. (Food Products)"    172.10333333333332
    1 201 "10. (Food Products)"                174.62
    1 202 "10. (Food Products)"     176.6966666666667
    1 203 "10. (Food Products)"    179.03666666666666
    1 204 "10. (Food Products)"    184.33666666666667
    1 205 "10. (Food Products)"    187.25333333333333
    1 206 "10. (Food Products)"    192.64999999999998
    1 207 "10. (Food Products)"    200.29999999999998
    1 208 "10. (Food Products)"     203.6966666666667
    1 209 "10. (Food Products)"    206.33666666666667
    1 210 "10. (Food Products)"    208.96666666666667
    1 211 "10. (Food Products)"                212.24
    1 212 "10. (Food Products)"                213.92
    1 213 "10. (Food Products)"    217.82666666666668
    1 214 "10. (Food Products)"    221.92333333333332
    1 215 "10. (Food Products)"                226.96
    1 216 "10. (Food Products)"    240.37666666666667
    1 217 "10. (Food Products)"    252.15333333333334
    1 218 "10. (Food Products)"     261.8933333333334
    1 219 "10. (Food Products)"    268.65999999999997
    1 220 "10. (Food Products)"    277.40333333333336
    1 221 "10. (Food Products)"    287.62333333333333
    1 222 "10. (Food Products)"    290.68666666666667
    1 223 "10. (Food Products)"    286.24666666666667
    1 224 "10. (Food Products)"    290.17333333333335
    1 225 "10. (Food Products)"     296.1433333333334
    1 226 "10. (Food Products)"    298.48333333333335
    1 227 "10. (Food Products)"                301.75
    1 228 "10. (Food Products)"     316.0233333333333
    1 229 "10. (Food Products)"    324.35999999999996
    1 230 "10. (Food Products)"                325.77
    1 231 "10. (Food Products)"                 321.2
    1 232 "10. (Food Products)"                331.76
    1 233 "10. (Food Products)"     356.1433333333334
    1 234 "10. (Food Products)"    386.85999999999996
    1 235 "10. (Food Products)"     409.2366666666667
    1 236 "10. (Food Products)"    415.94666666666666
    1 237 "10. (Food Products)"                452.77
    1 238 "10. (Food Products)"    454.25666666666666
    1 239 "10. (Food Products)"                454.31
    1 240 "10. (Food Products)"    479.60999999999996
    1 241 "10. (Food Products)"     499.8566666666666
    1 242 "10. (Food Products)"     507.2133333333333
    1 243 "10. (Food Products)"     558.7433333333332
    2 172 "12. (Tobacco Products)"  91.97333333333334
    2 173 "12. (Tobacco Products)"  96.22666666666665
    2 174 "12. (Tobacco Products)" 103.90666666666668
    2 175 "12. (Tobacco Products)" 107.88999999999999
    2 176 "12. (Tobacco Products)" 102.46333333333332
    2 177 "12. (Tobacco Products)"              102.2
    2 178 "12. (Tobacco Products)" 100.17333333333333
    2 179 "12. (Tobacco Products)" 109.41000000000001
    2 180 "12. (Tobacco Products)"  94.25333333333333
    2 181 "12. (Tobacco Products)"              91.11
    2 182 "12. (Tobacco Products)"             104.31
    2 183 "12. (Tobacco Products)"             110.56
    2 184 "12. (Tobacco Products)" 123.20666666666666
    2 185 "12. (Tobacco Products)" 123.69666666666667
    2 186 "12. (Tobacco Products)"             123.67
    2 187 "12. (Tobacco Products)"             123.67
    2 188 "12. (Tobacco Products)" 125.86333333333334
    2 189 "12. (Tobacco Products)" 125.84999999999998
    2 190 "12. (Tobacco Products)" 125.84999999999998
    2 191 "12. (Tobacco Products)" 127.03000000000002
    2 192 "12. (Tobacco Products)" 125.43333333333334
    2 193 "12. (Tobacco Products)" 125.42333333333333
    2 194 "12. (Tobacco Products)"             125.42
    2 195 "12. (Tobacco Products)" 125.42666666666668
    2 196 "12. (Tobacco Products)"             129.94
    2 197 "12. (Tobacco Products)" 136.77666666666667
    2 198 "12. (Tobacco Products)"             141.53
    2 199 "12. (Tobacco Products)"             141.53
    end
    format %tq qdate
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 1152 observations
    Use the count() option to list more
    Last edited by Muhammad Salahuddin; 17 Mar 2022, 03:36.

  • #2
    There is no year variable in your data example. As you have quarterly data, what would 100 correspond to? Average for 4 quarters of 2003?

    Comment


    • #3
      Originally posted by Nick Cox View Post
      There is no year variable in your data example. As you have quarterly data, what would 100 correspond to? Average for 4 quarters of 2003?
      Thanks for your reply. Starting and ending points of data are subsequently 2003 and 2020. The data represent the index value, and the average of 4 quarters (2003) is 100 because of the base year. Secondly, the index value 100 corresponds the sectoral inflation/Domestic Producer Price Index. Now, we want to make the base year 2015, its average of 4 quarters will be 100. Other values will be changed according to this base year.

      [CODE]
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str103 industry str4 year str12 month str40 inf float(id mdate qdate)
      "10. (Food Products)" "2003" "01-January" "93.42" 1 516 172
      "10. (Food Products)" "2003" "02-February" "94.49" 1 517 172
      "10. (Food Products)" "2003" "03-March" "95.63" 1 518 172
      "10. (Food Products)" "2003" "04-April" "98.09" 1 519 173
      "10. (Food Products)" "2003" "05-May" "99.03" 1 520 173
      "10. (Food Products)" "2003" "06-June" "99.27" 1 521 173
      "10. (Food Products)" "2003" "07-July" "100.68" 1 522 174
      "10. (Food Products)" "2003" "08-August" "101.79" 1 523 174
      "10. (Food Products)" "2003" "09-September" "103.24" 1 524 174
      "10. (Food Products)" "2003" "10-October" "103.58" 1 525 175
      "10. (Food Products)" "2003" "11-November" "105.05" 1 526 175
      "10. (Food Products)" "2003" "12-December" "105.72" 1 527 175
      "10. (Food Products)" "2004" "01-January" "108.36" 1 528 176
      "10. (Food Products)" "2004" "02-February" "108.42" 1 529 176
      "10. (Food Products)" "2004" "03-March" "108.67" 1 530 176
      "10. (Food Products)" "2004" "04-April" "109.8" 1 531 177
      "10. (Food Products)" "2004" "05-May" "112.65" 1 532 177
      "10. (Food Products)" "2004" "06-June" "116.13" 1 533 177
      "10. (Food Products)" "2004" "07-July" "115.46" 1 534 178
      "10. (Food Products)" "2004" "08-August" "117.2" 1 535 178
      "10. (Food Products)" "2004" "09-September" "119.21" 1 536 178
      "10. (Food Products)" "2004" "10-October" "119.49" 1 537 179
      "10. (Food Products)" "2004" "11-November" "119.81" 1 538 179
      "10. (Food Products)" "2004" "12-December" "120.14" 1 539 179
      "10. (Food Products)" "2005" "01-January" "120.16" 1 540 180
      "10. (Food Products)" "2005" "02-February" "122.86" 1 541 180
      "10. (Food Products)" "2005" "03-March" "122.77" 1 542 180
      "10. (Food Products)" "2005" "04-April" "122.8" 1 543 181
      "10. (Food Products)" "2005" "05-May" "123.08" 1 544 181
      "10. (Food Products)" "2005" "06-June" "122.71" 1 545 181
      "10. (Food Products)" "2005" "07-July" "122.67" 1 546 182
      "10. (Food Products)" "2005" "08-August" "122.3" 1 547 182
      "10. (Food Products)" "2005" "09-September" "121.78" 1 548 182
      "10. (Food Products)" "2005" "10-October" "121.25" 1 549 183
      "10. (Food Products)" "2005" "11-November" "121.37" 1 550 183
      "10. (Food Products)" "2005" "12-December" "121.38" 1 551 183
      "10. (Food Products)" "2006" "01-January" "121.78" 1 552 184
      "10. (Food Products)" "2006" "02-February" "121.93" 1 553 184
      "10. (Food Products)" "2006" "03-March" "121.48" 1 554 184
      "10. (Food Products)" "2006" "04-April" "121.66" 1 555 185
      "10. (Food Products)" "2006" "05-May" "119.68" 1 556 185
      "10. (Food Products)" "2006" "06-June" "120.14" 1 557 185
      "10. (Food Products)" "2006" "07-July" "121.29" 1 558 186
      "10. (Food Products)" "2006" "08-August" "121.37" 1 559 186
      "10. (Food Products)" "2006" "09-September" "122.9" 1 560 186
      "10. (Food Products)" "2006" "10-October" "123.83" 1 561 187
      "10. (Food Products)" "2006" "11-November" "124.16" 1 562 187
      "10. (Food Products)" "2006" "12-December" "126.07" 1 563 187
      "10. (Food Products)" "2007" "01-January" "127.28" 1 564 188
      "10. (Food Products)" "2007" "02-February" "128.42" 1 565 188
      "10. (Food Products)" "2007" "03-March" "129.13" 1 566 188
      "10. (Food Products)" "2007" "04-April" "129.67" 1 567 189
      "10. (Food Products)" "2007" "05-May" "131.51" 1 568 189

      Comment


      • #4
        These are now monthly data, but it's the same ball game. Let's show how to change the base year to 2006, and it's the same method for any other year. This is essentially the same method as in #1. so I can't say what went wrong there.

        Anyone wanting a reference for the egen trick could check out Section 9 of https://www.stata-journal.com/articl...article=dm0055

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str103 industry str4 year str12 month str40 inf float(id mdate qdate)
        "10. (Food Products)" "2003" "01-January" "93.42" 1 516 172
        "10. (Food Products)" "2003" "02-February" "94.49" 1 517 172
        "10. (Food Products)" "2003" "03-March" "95.63" 1 518 172
        "10. (Food Products)" "2003" "04-April" "98.09" 1 519 173
        "10. (Food Products)" "2003" "05-May" "99.03" 1 520 173
        "10. (Food Products)" "2003" "06-June" "99.27" 1 521 173
        "10. (Food Products)" "2003" "07-July" "100.68" 1 522 174
        "10. (Food Products)" "2003" "08-August" "101.79" 1 523 174
        "10. (Food Products)" "2003" "09-September" "103.24" 1 524 174
        "10. (Food Products)" "2003" "10-October" "103.58" 1 525 175
        "10. (Food Products)" "2003" "11-November" "105.05" 1 526 175
        "10. (Food Products)" "2003" "12-December" "105.72" 1 527 175
        "10. (Food Products)" "2004" "01-January" "108.36" 1 528 176
        "10. (Food Products)" "2004" "02-February" "108.42" 1 529 176
        "10. (Food Products)" "2004" "03-March" "108.67" 1 530 176
        "10. (Food Products)" "2004" "04-April" "109.8" 1 531 177
        "10. (Food Products)" "2004" "05-May" "112.65" 1 532 177
        "10. (Food Products)" "2004" "06-June" "116.13" 1 533 177
        "10. (Food Products)" "2004" "07-July" "115.46" 1 534 178
        "10. (Food Products)" "2004" "08-August" "117.2" 1 535 178
        "10. (Food Products)" "2004" "09-September" "119.21" 1 536 178
        "10. (Food Products)" "2004" "10-October" "119.49" 1 537 179
        "10. (Food Products)" "2004" "11-November" "119.81" 1 538 179
        "10. (Food Products)" "2004" "12-December" "120.14" 1 539 179
        "10. (Food Products)" "2005" "01-January" "120.16" 1 540 180
        "10. (Food Products)" "2005" "02-February" "122.86" 1 541 180
        "10. (Food Products)" "2005" "03-March" "122.77" 1 542 180
        "10. (Food Products)" "2005" "04-April" "122.8" 1 543 181
        "10. (Food Products)" "2005" "05-May" "123.08" 1 544 181
        "10. (Food Products)" "2005" "06-June" "122.71" 1 545 181
        "10. (Food Products)" "2005" "07-July" "122.67" 1 546 182
        "10. (Food Products)" "2005" "08-August" "122.3" 1 547 182
        "10. (Food Products)" "2005" "09-September" "121.78" 1 548 182
        "10. (Food Products)" "2005" "10-October" "121.25" 1 549 183
        "10. (Food Products)" "2005" "11-November" "121.37" 1 550 183
        "10. (Food Products)" "2005" "12-December" "121.38" 1 551 183
        "10. (Food Products)" "2006" "01-January" "121.78" 1 552 184
        "10. (Food Products)" "2006" "02-February" "121.93" 1 553 184
        "10. (Food Products)" "2006" "03-March" "121.48" 1 554 184
        "10. (Food Products)" "2006" "04-April" "121.66" 1 555 185
        "10. (Food Products)" "2006" "05-May" "119.68" 1 556 185
        "10. (Food Products)" "2006" "06-June" "120.14" 1 557 185
        "10. (Food Products)" "2006" "07-July" "121.29" 1 558 186
        "10. (Food Products)" "2006" "08-August" "121.37" 1 559 186
        "10. (Food Products)" "2006" "09-September" "122.9" 1 560 186
        "10. (Food Products)" "2006" "10-October" "123.83" 1 561 187
        "10. (Food Products)" "2006" "11-November" "124.16" 1 562 187
        "10. (Food Products)" "2006" "12-December" "126.07" 1 563 187
        "10. (Food Products)" "2007" "01-January" "127.28" 1 564 188
        "10. (Food Products)" "2007" "02-February" "128.42" 1 565 188
        "10. (Food Products)" "2007" "03-March" "129.13" 1 566 188
        "10. (Food Products)" "2007" "04-April" "129.67" 1 567 189
        "10. (Food Products)" "2007" "05-May" "131.51" 1 568 189
        end 
        
        destring inf year, replace 
        
        bysort industry : egen mean2006 = mean(cond(year == 2006, inf, .))
        
        gen wanted = 100 * inf / mean2006
        
        list, sepby(year)
        
             +-----------------------------------------------------------------------------------------------+
             |            industry   year          month      inf   id   mdate   qdate   mean2006     wanted |
             |-----------------------------------------------------------------------------------------------|
          1. | 10. (Food Products)   2003     01-January    93.42    1     516     172   122.1908   76.45418 |
          2. | 10. (Food Products)   2003    02-February    94.49    1     517     172   122.1908   77.32986 |
          3. | 10. (Food Products)   2003       03-March    95.63    1     518     172   122.1908   78.26283 |
          4. | 10. (Food Products)   2003       04-April    98.09    1     519     173   122.1908   80.27607 |
          5. | 10. (Food Products)   2003         05-May    99.03    1     520     173   122.1908   81.04536 |
          6. | 10. (Food Products)   2003        06-June    99.27    1     521     173   122.1908   81.24178 |
          7. | 10. (Food Products)   2003        07-July   100.68    1     522     174   122.1908   82.39571 |
          8. | 10. (Food Products)   2003      08-August   101.79    1     523     174   122.1908   83.30412 |
          9. | 10. (Food Products)   2003   09-September   103.24    1     524     174   122.1908   84.49079 |
         10. | 10. (Food Products)   2003     10-October   103.58    1     525     175   122.1908   84.76904 |
         11. | 10. (Food Products)   2003    11-November   105.05    1     526     175   122.1908   85.97208 |
         12. | 10. (Food Products)   2003    12-December   105.72    1     527     175   122.1908    86.5204 |
             |-----------------------------------------------------------------------------------------------|
         13. | 10. (Food Products)   2004     01-January   108.36    1     528     176   122.1908   88.68095 |
         14. | 10. (Food Products)   2004    02-February   108.42    1     529     176   122.1908   88.73006 |
         15. | 10. (Food Products)   2004       03-March   108.67    1     530     176   122.1908   88.93465 |
         16. | 10. (Food Products)   2004       04-April    109.8    1     531     177   122.1908   89.85944 |
         17. | 10. (Food Products)   2004         05-May   112.65    1     532     177   122.1908   92.19186 |
         18. | 10. (Food Products)   2004        06-June   116.13    1     533     177   122.1908   95.03986 |
         19. | 10. (Food Products)   2004        07-July   115.46    1     534     178   122.1908   94.49154 |
         20. | 10. (Food Products)   2004      08-August    117.2    1     535     178   122.1908   95.91554 |
         21. | 10. (Food Products)   2004   09-September   119.21    1     536     178   122.1908   97.56051 |
         22. | 10. (Food Products)   2004     10-October   119.49    1     537     179   122.1908   97.78966 |
         23. | 10. (Food Products)   2004    11-November   119.81    1     538     179   122.1908   98.05154 |
         24. | 10. (Food Products)   2004    12-December   120.14    1     539     179   122.1908   98.32162 |
             |-----------------------------------------------------------------------------------------------|
         25. | 10. (Food Products)   2005     01-January   120.16    1     540     180   122.1908   98.33798 |
         26. | 10. (Food Products)   2005    02-February   122.86    1     541     180   122.1908   100.5476 |
         27. | 10. (Food Products)   2005       03-March   122.77    1     542     180   122.1908    100.474 |
         28. | 10. (Food Products)   2005       04-April    122.8    1     543     181   122.1908   100.4985 |
         29. | 10. (Food Products)   2005         05-May   123.08    1     544     181   122.1908   100.7277 |
         30. | 10. (Food Products)   2005        06-June   122.71    1     545     181   122.1908   100.4249 |
         31. | 10. (Food Products)   2005        07-July   122.67    1     546     182   122.1908   100.3921 |
         32. | 10. (Food Products)   2005      08-August    122.3    1     547     182   122.1908   100.0893 |
         33. | 10. (Food Products)   2005   09-September   121.78    1     548     182   122.1908   99.66378 |
         34. | 10. (Food Products)   2005     10-October   121.25    1     549     183   122.1908   99.23003 |
         35. | 10. (Food Products)   2005    11-November   121.37    1     550     183   122.1908   99.32824 |
         36. | 10. (Food Products)   2005    12-December   121.38    1     551     183   122.1908   99.33642 |
             |-----------------------------------------------------------------------------------------------|
         37. | 10. (Food Products)   2006     01-January   121.78    1     552     184   122.1908   99.66378 |
         38. | 10. (Food Products)   2006    02-February   121.93    1     553     184   122.1908   99.78654 |
         39. | 10. (Food Products)   2006       03-March   121.48    1     554     184   122.1908   99.41826 |
         40. | 10. (Food Products)   2006       04-April   121.66    1     555     185   122.1908   99.56557 |
         41. | 10. (Food Products)   2006         05-May   119.68    1     556     185   122.1908   97.94515 |
         42. | 10. (Food Products)   2006        06-June   120.14    1     557     185   122.1908   98.32162 |
         43. | 10. (Food Products)   2006        07-July   121.29    1     558     186   122.1908   99.26276 |
         44. | 10. (Food Products)   2006      08-August   121.37    1     559     186   122.1908   99.32824 |
         45. | 10. (Food Products)   2006   09-September    122.9    1     560     186   122.1908   100.5804 |
         46. | 10. (Food Products)   2006     10-October   123.83    1     561     187   122.1908   101.3415 |
         47. | 10. (Food Products)   2006    11-November   124.16    1     562     187   122.1908   101.6115 |
         48. | 10. (Food Products)   2006    12-December   126.07    1     563     187   122.1908   103.1747 |
             |-----------------------------------------------------------------------------------------------|
         49. | 10. (Food Products)   2007     01-January   127.28    1     564     188   122.1908   104.1649 |
         50. | 10. (Food Products)   2007    02-February   128.42    1     565     188   122.1908   105.0979 |
         51. | 10. (Food Products)   2007       03-March   129.13    1     566     188   122.1908    105.679 |
         52. | 10. (Food Products)   2007       04-April   129.67    1     567     189   122.1908   106.1209 |
         53. | 10. (Food Products)   2007         05-May   131.51    1     568     189   122.1908   107.6267 |
             +-----------------------------------------------------------------------------------------------+

        Comment

        Working...
        X