Announcement

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

  • Using A Loop for a Simple Division

    Hi,

    My apologies for bothering you with such a simple query. I am trying to do a simple division. I have 100+ variables. I have years ranging from 2016 to 2021. Several variables with prefix "rev" and "exp".

    I want to generate ratios such as:
    share_variable_year = rev_variable_year/exp_variable_year * 100

    Code:
    Example:
    gen share_soc_services2016 = rev_soc_services2016/exp_soc_services2016 * 100
    Manually it takes a lot of time. I was trying with loops. But could not do. Would appreciate it if anybody could kindly help with this.
    I have provided the data example below.

    Code:
    * Example generated by -dataex-. For more info, type help dataex.
    
    
    clear
    input str2 stcode double(rev_soc_services2016 rev_soc_services2017 rev_mph2016 rev_mph2017 exp_soc_services2016 exp_soc_services2017 exp_mph2016 exp_mph2017)
    "AP"         1617.8866         -569.6508           83.3561          171.7518        50227.8676        59159.3498         4694.1987         4450.0773
    "AS" 44.53400000000001 74.01769999999998           12.3265           22.6839         22673.142        21373.1693         2904.3253         3909.8413
    "BR" 75.55640000000001          124.0436            39.938           54.5251        40736.6335        45769.4657         4189.1222         5064.1755
    "CT"          145.5563           154.619           46.4967            52.557        21341.6112        24371.5905         2768.8327         3376.4992
    "GJ"           3226.12           1183.74 981.9799999999999            173.81          44926.02             49039           5208.13            5575.9
    "HR"          1455.414 3896.788500000001           31.1714          189.3393        25473.4883        28061.3383         2630.7664         2903.6326
    "JH"       368.1921491          673.0495        20.5277172            14.224        18554.2114        19574.4642         1878.4059         2458.1713
    "KA"          485.8977          725.8508          152.8966          364.2229        54549.2409        58652.3505         5510.7134          6354.081
    "KL" 539.5639060999999       484.2157954       218.2200626       181.5287666     33764.7176421     35876.2711827       5219.097365      5599.2505825
    "MH"          2338.763         3221.5086          448.9098          474.5921 90281.99080000001 93054.30140000001 9436.386600000002        10782.2731
    "MP"         2338.6357         1608.4078          167.0393          128.9848        47942.4287        58346.1741         5002.7372         6005.6748
    "OD"          208.9032          208.6337           50.8295            57.794        27599.9527 29556.82729999999          3735.717         3805.5595
    "PB" 416.1993000000001 505.3808999999999          135.4685          253.3449        15672.0959        15469.7428         2670.9508         2533.9534
    "RJ"         1324.2686         1326.7841          125.3859          130.6745        49371.6819 53064.07059999998 5453.674199999999 6696.968100000001
    "TN"         2806.1828         2767.9652           422.796 516.7006000000001        55297.2662 59790.17369999999         6160.9064         7991.2386
    "TS"         1993.0317 711.4556999999998          183.3157          261.5749         35285.562 37259.29060000001         3343.1856         3408.0746
    "UP"        14653.2236         1571.1239          185.7238          260.4981        91861.1231         84251.683         7898.2315         9499.0179
    "WB" 380.3331000000001          341.5845          134.7799          210.4753        57066.8386 59590.75519999999 6355.144399999999 7204.097299999999
    end

    Thank you in advance.


  • #2
    Code:
    forvalues n = 2016/2017 {
     foreach s in soc_services mph {
     gen share_`s'`n'=rev_`s'`n'/exp_`s'`n'*100
     }
    }

    Comment


    • #3
      Thank you a lot, Chen Samulsion. That worked magic!

      Comment


      • #4
        Chen Samulsion 's code did what you wanted, but long term you'd be better off with a reshape long.

        For how to get the stubs, see https://www.stata.com/support/faqs/d...-with-reshape/


        Code:
        clear
        input str2 stcode double(rev_soc_services2016 rev_soc_services2017 rev_mph2016 rev_mph2017 exp_soc_services2016 exp_soc_services2017 exp_mph2016 exp_mph2017)
        "AP"         1617.8866         -569.6508           83.3561          171.7518        50227.8676        59159.3498         4694.1987         4450.0773
        "AS" 44.53400000000001 74.01769999999998           12.3265           22.6839         22673.142        21373.1693         2904.3253         3909.8413
        "BR" 75.55640000000001          124.0436            39.938           54.5251        40736.6335        45769.4657         4189.1222         5064.1755
        "CT"          145.5563           154.619           46.4967            52.557        21341.6112        24371.5905         2768.8327         3376.4992
        "GJ"           3226.12           1183.74 981.9799999999999            173.81          44926.02             49039           5208.13            5575.9
        "HR"          1455.414 3896.788500000001           31.1714          189.3393        25473.4883        28061.3383         2630.7664         2903.6326
        "JH"       368.1921491          673.0495        20.5277172            14.224        18554.2114        19574.4642         1878.4059         2458.1713
        "KA"          485.8977          725.8508          152.8966          364.2229        54549.2409        58652.3505         5510.7134          6354.081
        "KL" 539.5639060999999       484.2157954       218.2200626       181.5287666     33764.7176421     35876.2711827       5219.097365      5599.2505825
        "MH"          2338.763         3221.5086          448.9098          474.5921 90281.99080000001 93054.30140000001 9436.386600000002        10782.2731
        "MP"         2338.6357         1608.4078          167.0393          128.9848        47942.4287        58346.1741         5002.7372         6005.6748
        "OD"          208.9032          208.6337           50.8295            57.794        27599.9527 29556.82729999999          3735.717         3805.5595
        "PB" 416.1993000000001 505.3808999999999          135.4685          253.3449        15672.0959        15469.7428         2670.9508         2533.9534
        "RJ"         1324.2686         1326.7841          125.3859          130.6745        49371.6819 53064.07059999998 5453.674199999999 6696.968100000001
        "TN"         2806.1828         2767.9652           422.796 516.7006000000001        55297.2662 59790.17369999999         6160.9064         7991.2386
        "TS"         1993.0317 711.4556999999998          183.3157          261.5749         35285.562 37259.29060000001         3343.1856         3408.0746
        "UP"        14653.2236         1571.1239          185.7238          260.4981        91861.1231         84251.683         7898.2315         9499.0179
        "WB" 380.3331000000001          341.5845          134.7799          210.4753        57066.8386 59590.75519999999 6355.144399999999 7204.097299999999
        end
        
        unab stubs : *2016 
        local stubs : subinstr local stubs "2016" "", all 
        reshape long `stubs', i(stcode) j(year)
        
        
        list 
        
             +---------------------------------------------------------------+
             | stcode   year   rev_soc~s     rev_mph   exp_soc~s     exp_mph |
             |---------------------------------------------------------------|
          1. |     AP   2016   1617.8866     83.3561   50227.868   4694.1987 |
          2. |     AP   2017   -569.6508    171.7518    59159.35   4450.0773 |
          3. |     AS   2016      44.534     12.3265   22673.142   2904.3253 |
          4. |     AS   2017     74.0177     22.6839   21373.169   3909.8413 |
          5. |     BR   2016     75.5564      39.938   40736.634   4189.1222 |
             |---------------------------------------------------------------|
          6. |     BR   2017    124.0436     54.5251   45769.466   5064.1755 |
          7. |     CT   2016    145.5563     46.4967   21341.611   2768.8327 |
          8. |     CT   2017     154.619      52.557    24371.59   3376.4992 |
          9. |     GJ   2016     3226.12      981.98    44926.02     5208.13 |
         10. |     GJ   2017     1183.74      173.81       49039      5575.9 |
             |---------------------------------------------------------------|
         11. |     HR   2016    1455.414     31.1714   25473.488   2630.7664 |
         12. |     HR   2017   3896.7885    189.3393   28061.338   2903.6326 |
         13. |     JH   2016   368.19215   20.527717   18554.211   1878.4059 |
         14. |     JH   2017    673.0495      14.224   19574.464   2458.1713 |
         15. |     KA   2016    485.8977    152.8966   54549.241   5510.7134 |
             |---------------------------------------------------------------|
         16. |     KA   2017    725.8508    364.2229   58652.351    6354.081 |
         17. |     KL   2016   539.56391   218.22006   33764.718   5219.0974 |
         18. |     KL   2017    484.2158   181.52877   35876.271   5599.2506 |
         19. |     MH   2016    2338.763    448.9098   90281.991   9436.3866 |
         20. |     MH   2017   3221.5086    474.5921   93054.301   10782.273 |
             |---------------------------------------------------------------|
         21. |     MP   2016   2338.6357    167.0393   47942.429   5002.7372 |
         22. |     MP   2017   1608.4078    128.9848   58346.174   6005.6748 |
         23. |     OD   2016    208.9032     50.8295   27599.953    3735.717 |
         24. |     OD   2017    208.6337      57.794   29556.827   3805.5595 |
         25. |     PB   2016    416.1993    135.4685   15672.096   2670.9508 |
             |---------------------------------------------------------------|
         26. |     PB   2017    505.3809    253.3449   15469.743   2533.9534 |
         27. |     RJ   2016   1324.2686    125.3859   49371.682   5453.6742 |
         28. |     RJ   2017   1326.7841    130.6745   53064.071   6696.9681 |
         29. |     TN   2016   2806.1828     422.796   55297.266   6160.9064 |
         30. |     TN   2017   2767.9652    516.7006   59790.174   7991.2386 |
             |---------------------------------------------------------------|
         31. |     TS   2016   1993.0317    183.3157   35285.562   3343.1856 |
         32. |     TS   2017    711.4557    261.5749   37259.291   3408.0746 |
         33. |     UP   2016   14653.224    185.7238   91861.123   7898.2315 |
         34. |     UP   2017   1571.1239    260.4981   84251.683   9499.0179 |
         35. |     WB   2016    380.3331    134.7799   57066.839   6355.1444 |
             |---------------------------------------------------------------|
         36. |     WB   2017    341.5845    210.4753   59590.755   7204.0973 |
             +---------------------------------------------------------------+
        Then you need not two loops, but one.

        Comment


        • #5
          Thank you, Nick Cox, for kindly providing the alternate solution.

          Comment

          Working...
          X