Announcement

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

  • Creating a table using a for loop

    I'm having trouble using a foreach loop to construct a table. I have 4 variables, "debtgdp, dRGDP, Year, and Country." The forloop im trying to construct must calculate the mean and standard error of GDP growth (variable 'dRGDP') for the following debt/GDP buckets (variable 'debtgdp'): 0-30, 31-60, 61-90, 90+. The loop then must put these results into a 5 column table with countries in column 1, and the results for the debt buckets in columns 2-5, with the standard errors in parentheses under the means.

    The part I'm having most trouble with is sorting my data into these debt buckets with the loop. I'm also unsure of which table command to use. I'm a beginner with stata especially when it comes to loops, so any kind of advice would be appreciated.

  • #2
    Hi Kingfish,

    Welcome to Statalist!

    Could you possibly share what code you have so far? There are many different ways of making tables in Stata and the bucketing you refer to could happen any number of ways depending on your data's structure. If you could provide a short data example using dataex (ssc install dataex) as well as whatever code you have so far that would be a great help

    There's also a strong suggestion in Statalist's FAQs for members to use real first and last names. I don't want to presume that Kingfish is not your real name, but if it is not then please do read the FAQs and follow the instructions to change it.

    Comment


    • #3
      Hi Chris,

      I apologize for the name confusion, I was unsure of whether or not I should use my real name and I usually try to avoid using my real name on forums. I will get on changing that now. I have included an example dataset below:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long Country double(Year dRGDP debtgdp)
      1 1946   -3.557951482479782  190.4190800681431
      1 1947    2.459474566797093 177.32137135533483
      1 1948    6.437534097108566 148.92981051507874
      1 1949    6.611993849308062 125.82869855394883
      1 1950    6.920201241847579 109.80939799962258
      1 1951     4.27261154812808  87.09447922595334
      1 1952     .904651599574291  86.06643817558661
      1 1953    3.119280285404069  79.86502210844776
      1 1954   6.2168138265068285  76.84670288697134
      1 1955    5.462089670603554  74.98050194980502
      1 1956    3.441608142766972   71.8182657678456
      1 1957   2.0030116571903234   67.7237634682277
      1 1958   4.8029321557198745  66.04975124378109
      1 1959    6.156573690665557  62.38845144356955
      1 1960   4.1912126376957515   48.3363802559415
      1 1961    .6894658835153944  49.37811382685658
      1 1962    6.248841494662694  50.33076074972437
      1 1963   6.1255695578999125  47.58753094528369
      1 1964    6.841499618036417  45.01152604887045
      1 1965    5.107341973789015  43.73903743315508
      1 1966    2.783063953638565  42.48123302986743
      1 1967    6.751673466652153  40.53448088814957
      1 1968    5.878890615435317  39.49426658712799
      1 1969    6.081697093682603  36.80077642491618
      1 1970   6.3597870288852665  34.96359062624123
      1 1971    4.448824070424395 32.289871721233006
      1 1972    2.805801549763509 30.721744270046628
      1 1973    5.421130233155713 27.361466800282077
      1 1974   2.4791949580417194  22.36639439906651
      1 1975     2.70746265275843  23.63684735616894
      1 1976    4.031053058163825 22.982349085813727
      1 1977    1.046756908595592 23.638910316268117
      1 1978   2.9010820705680063 24.732891751109314
      1 1979    5.265974799168127  20.87219511412154
      1 1980     1.99840205311963 19.124087591240876
      1 1981    4.155751637817917  16.17684498121643
      1 1982 -.009740006753067299 15.043615122864965
      1 1983   -.5260115982310465  17.57969557725445
      1 1984   6.4144998117670715    20.062923963846
      1 1985   5.7161962643196285 21.424017003188098
      1 1986    2.110579592120976 21.491707707043357
      1 1987    4.396280958857912 19.993912351237263
      1 1988    4.030412731248356  16.29359341140772
      1 1989     4.57494906645084 12.498329765800415
      1 1990   1.6044395220208063 10.331201388837659
      1 1991  -1.2501169676085944   9.30832139271476
      1 1992   2.1122873748187887 10.865716915217309
      1 1993    3.897211811785084 13.814962432037154
      1 1994    5.013538669825679 15.918474881706379
      1 1995    3.488831888171151 18.383455868401132
      1 1996    4.281607040896951 17.775323368892025
      1 1997   3.9969968599765204  17.41232457350337
      1 1998    5.051379310803283 14.586137695048864
      1 1999    4.401723734251317 13.216049263804075
      1 2000    3.426129648149834 11.106017870849625
      1 2001   2.0893402157554863   9.23978259677442
      1 2002    4.247348492676362  8.227777925016927
      1 2003    2.965939764914216  7.031335038723304
      1 2004    3.842278648897257  6.271350951371776
      1 2005    2.759194654652619  5.892013342543951
      1 2006   2.8403720339076566  5.392566362747641
      1 2007    4.048780297625321  4.905430103446052
      1 2008   2.3542239641489493  4.677887066339233
      1 2009    .7322497391686333  8.418269841600147
      2 1948   27.329192546583858  35.20228329018372
      2 1949    18.90243902439024 26.448141371340522
      2 1950    12.40896183968976 22.144398212359377
      2 1951    6.839934635436928 15.264331210191083
      2 1952   .08739985433356967 13.580401224923442
      2 1953    4.351622762334451 16.565805896537935
      2 1954   10.219665271966537 14.268099837592956
      2 1955    11.05311442219481 11.894731930381223
      2 1956     6.87936191425722  10.92381911234164
      2 1957    6.124733475479749  8.304269073657247
      2 1958   3.6516148475563837 11.592758073670883
      2 1959   2.8421205660011717 13.519904322569623
      2 1960    8.231829426316416 13.568510226458319
      2 1961    5.309214392999406   12.3920535956318
      2 1962    2.422588780023971 11.966269645280194
      2 1963    4.070635721493443 12.088617058735572
      2 1964    6.001900440204011 12.043660418963617
      2 1965    2.875855255936477 11.471399594320486
      2 1966    5.628267596116232 10.978289204185753
      2 1967   3.0404552265189677 12.107069080214279
      2 1968    4.444081365901487 12.984291487420155
      2 1969    6.288619475948387 13.016119402985074
      2 1970    7.123408639340645 10.583438065340003
      2 1971    5.112317098303221   9.27693842047567
      2 1972    6.208663372807366  8.938241059326453
      2 1973    4.887023727478579   9.18619339405649
      2 1974   3.9435859528083927  8.875890683350308
      2 1975   -.3623515600307048 12.799352405047857
      2 1976    4.577021990098218 15.181059141509003
      2 1977    4.676785075888068  16.42551420617569
      2 1978  -.36326378539492943 18.856047724055625
      2 1979    5.455319767743805 23.588178013241784
      2 1980                2.314 25.201147136142755
      2 1981                -.099 26.762284859067098
      2 1982                1.908 28.842310731049867
      2 1983                2.804  32.97752870242158
      end
      label values Country Country
      label def Country 1 "Australia", modify
      label def Country 2 "Austria", modify
      I have creating this loop in numerous different ways without success. The latest code I have tried is:

      foreach Country of varlist debtgdp dRGDP {
      bysort Country: ci means dRGDP if debtgd <= 30
      local a = r(mean)
      local ao = r(se)
      }

      foreach Country of varlist debtgdp dRGDP {
      bysort Country: ci means dRGDP if inrange(debtgd,31,60)
      local b = r(mean)
      local bo = r(se)
      }

      foreach Country of varlist debtgdp dRGDP {
      bysort Country: ci means dRGDP if inrange(debtgd,61,90)
      local c = r(mean)
      local co = r(se)
      }

      foreach Country of varlist debtgdp dRGDP {
      bysort Country: ci means dRGDP if debtgdp > 90
      local d = r(mean)
      local do = r(se)
      }

      Where I tried to store the statistics in locals. From there I was going to fill a matrix with the locals for my table but that clearly wouldn't create the table I am looking for. I have been working on this for days and I'm really stuck. Thank you for the response.

      Comment


      • #4
        If you can live without the parentheses around the standard errors, it's actually fairly simple, and no loops are needed:

        Code:
        gen int bucket = floor(debtgdp/30)
        label define bucket    0    "0-30"    ///
                            1    "30-60"    ///
                            2    "60-90" ///
                            3    "90+"
        replace bucket = 3 if bucket > 3
        label values bucket bucket
        
        table Country bucket, c(mean dRGDP sem dRGDP)
        Since the whole approach is unnecessary, I won't walk through the problems in the code you tried.

        What I will point out is that your approach to creating the buckets is flawed: it would work just fine if debtgdp were an integer variable, but because it contains non-integer numbers, you cannot divided it into 0-30, 31-60, 61-90 without completely overlooking any observations with values of debtgdp that fall between 30 and 31, or 60 and 61. A different approach altogether is needed. Fortunately, this sort of thing lends itself to solutions involving dividing the variable by the width of the bin and then applying a -floor()- or -ceil()- function (and sometimes you have to add or subtract 1 to get exactly what you want.)

        Comment


        • #5
          Hi Clyde,

          Thank you for the help. I figured that creating a loop for this may not be necessary. I have just been trying to practice using for loops lately because I know I'm going to need them when working on more complex tasks. I do appreciate the advice on the floor and ceiling functions, that never crossed my mind. The reason I needed the standard errors in parentheses is that I'm trying to replicate a table that I found in a textbook, but I'm sure I can just edit the table in LaTeX for that. Thanks again.

          Comment


          • #6
            A small twist on Clyde's code is

            Code:
             
             gen int bucket = 30 * floor(debtgdp/30)
            Then labels are at choice because each value is the lower limit of the bin.

            Comment

            Working...
            X