Announcement

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

  • Loop for adding values of every observation within variable

    Hello statalisters,

    I regard my puzzle as symple but I cannot get my head around it and I have already check the Stata's help guides and this forum in the search of a solution but I cannot make it work regardless.

    Basically I have a variable with 11 missing values and 11 missing observations in it. As follows the data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long date str10 cusip double(ret year) str2 gsector float(me btm) long IPOdate float(IPO fulldate month first_date fullfirst_date n portf_ret portf_ret1 prod portf_bh portf_annualmean_bh wr bhar nvals mean_bhar sd_bhar t)
    180 "65334H10"    .3333333432674408 1975 "10"    57.392 1.2116153    . 0 5479  1 180 5479  1  .23957734 1.2395773  57005.22  57004.22 1.4907275         .          . 1 -13839.544 27655.84 -1.6597064
    180 "12803020"   .06896551698446274 1975 "10"  9.296125         . 5479 1 5479  1 180 5479  1  .06896552 1.0689656 28.504797 27.504797  .3220356  .2160258  -56976.72 0 -13839.544 27655.84 -1.6597064
    180 "13442260"    .2083333283662796 1975 "15" 16.250875  2.059226    . 0 5479  1 180 5479  1   .2832149  1.283215  5515.046  5514.046 1.0501978         .          . 1 -13839.544 27655.84 -1.6597064
    190 "53631510"  -.09433962404727936 1975 "15" 156.58376 .56269795 5783 1 5783 11 190 5783  1 -.09433962  .9056604  7.262182  6.262182  .1796566  .1710693  -5507.784 0 -13839.544 27655.84 -1.6597064
    180 "55261510"    .4615384638309479 1975 "20"  10.55925  1.544468    . 0 5479  1 180 5479  1   .2984893 1.2984893  916.9833  915.9833  .7654827         .          . 1 -13839.544 27655.84 -1.6597064
    180 "48120610"   .16714597729587966 1975 "20"         .         . 5479 1 5479  1 180 5479  1  .32649085  1.326491  52.41497  51.41497  .3908745  .5106249  -864.5683 0 -13839.544 27655.84 -1.6597064
    180 "12216930"                  -.5 1975 "25"  161.9685         .    . 0 5479  1 180 5479  1   .3238313 1.3238313  1094.934  1093.934  .7917703         .          . 1 -13839.544 27655.84 -1.6597064
    180 "75764210"    .8333336622223755 1975 "25"         .         . 5479 1 5479  1 180 5479  1    .405184  1.405184 199.38013 198.38013 .55467695  .7005529  -895.5538 0 -13839.544 27655.84 -1.6597064
    180 "83240710"    .3035714328289032 1975 "30"  21.62625 1.1024528    . 0 5479  1 180 5479  1   .2895239  1.289524 1037.2871 1036.2871  .7837127         .          . 1 -13839.544 27655.84 -1.6597064
    180 "92241710"    .4181825148568985 1975 "30"         .         . 5479 1 5479  1 180 5479  1   .4181825 1.4181825  8.522838  7.522838 .19549753  .2494505 -1028.7643 0 -13839.544 27655.84 -1.6597064
    180 "18946810"    .4399999976158142 1975 "35"    26.235  2.864055    . 0 5479  1 180 5479  1    .296936  1.296936 1828.7367 1827.7367  .8700186         .          . 1 -13839.544 27655.84 -1.6597064
    180 "20557T10"    1.333999584839753 1975 "35"         .         . 5479 1 5479  1 180 5479  1   .4148225 1.4148226  2191.395  2190.395  .8984247   1.03265   362.6583 0 -13839.544 27655.84 -1.6597064
    180 "25466010"   .30000001192092896 1975 "40"  1.613625     2.948    . 0 5479  1 180 5479  1   .3001843 1.3001842 1792.8048 1791.8048  .8669288         .          . 1 -13839.544 27655.84 -1.6597064
    180 "86209910"    .3164556963050795 1975 "40"  41.00196         . 5479 1 5479  1 180 5479  1  .22502947 1.2250295 124.57706 123.57706  .4949265  .5708964 -1668.2278 0 -13839.544 27655.84 -1.6597064
    180 "01163410"    .2857142155102015 1975 "45"         .         .    . 0 5479  1 180 5479  1    .353499  1.353499  83767.56  83766.56 1.5719132         .          . 1 -13839.544 27655.84 -1.6597064
    180 "20681410"     .282258064516129 1975 "45"         .         . 5479 1 5479  1 180 5479  1   .6367216 1.6367216 3376.6794 3375.6794  .9680713  .6158554  -80390.88 0 -13839.544 27655.84 -1.6597064
    180 "78199Y10"   1.3333333333333335 1975 "50"     3.682         .    . 0 5479  1 180 5479  1    .329451  1.329451 1298.3778 1297.3778  .8173982         .          . 1 -13839.544 27655.84 -1.6597064
    180 "19687910"   .19047491412413664 1975 "50"         .         . 5479 1 5479  1 180 5479  1   .1904749 1.1904749 290.60837 289.60837  .6042634  .7392522 -1007.7694 0 -13839.544 27655.84 -1.6597064
    180 "83541510"   .04093567281961441 1975 "55"  447.5365  .8403047    . 0 5479  1 180 5479  1    .255589  1.255589  2543.142  2542.142  .9221217         .          . 1 -13839.544 27655.84 -1.6597064
    204 "19567410"   .10869564861059189 1977 "55"    15.351         . 5813 1 6210  1 191 5813 14  .10869565 1.1086956  1.744596   .744596 .04746915 .05147818  -2541.397 0 -13839.544 27655.84 -1.6597064
    180 "88654610"   .20930232107639313 1975 "60"    14.898  1.415845    . 0 5479  1 180 5479  1   .3477683 1.3477683 1722.6477 1721.6477  .8607286         .          . 1 -13839.544 27655.84 -1.6597064
    247 "03814410" -.038461538461538436 1980 "60"         .         . 7518 1 7518  8 247 7518  1 -.03846154  .9615384  6.674772  5.674772  .1713941 .19912677  -1715.973 0 -13839.544 27655.84 -1.6597064
    end
    format %tm date
    format %td IPOdate
    format %d fulldate
    format %d fullfirst_date


    I simply want to create a new variable that contains the sum of ((each observation within "bhar" minus its mean) to the third power). Let me be more illustrative with a picture:

    Click image for larger version

Name:	Screenshot (361).png
Views:	1
Size:	1.8 KB
ID:	1492012


    Now, I have tried the following loop albeit unsucessfully:

    Code:
    gen x=.
    forvalues i = 1/11 {
      replace x = x + (bhar - mean_bhar)^3 if bhar==`i'
    }
    Variable "x" should contain 11 missing values and other 11 values that are all the same and equal to the result of the previous operation.


    Thank you for your attention. I much appreciate your help.


  • #2
    If I understood right, you won't need a loop for that matter. Just - gen myvar =(bhar - mean_bhar)^3 - may do the trick. With regards to the missing values, I fail to understand what code you wish. Also, the "sum" of the values of the new variable wasn't clear to me. That said, say, if it is by "id", you won't need a loop anyway, for you may get that with - egen - command, plus total(). Hopefully that helps.
    Best regards,

    Marcos

    Comment


    • #3
      If this is a step towards skewness, then check out summarize and egen.

      Comment


      • #4
        Thank you Marcos and Nick for your answers.

        Marcos, you are completely right, I do not need a loop to achieve my purpose. Your simple arithmetic commands helped me to achieve the purpose I wanted.

        My mission was actually to calculate the bootstrapped skewness-adjusted t-statistic (Johnson, 1978; Lyon et al., 1999). I realize that for it I must follow a bootstrapping process; thus, finally I have calculate the skewness-adjusted t-stat by using the following command and program:

        Code:
        hallt bhar, bs reps(1000) size(1) saving ("X:\result.dta")
        I hope that this can also be helpful for some other users. Thank you for your time.



        Comment


        • #5
          Thanks for the closure.

          hallt is from SSC, as you are asked to explain:

          Code:
          net describe hallt-skewt, from(http://fmwww.bc.edu/RePEc/bocode/h)
          Please note http://www.statalist.org/forums/help#stata and http://www.statalist.org/forums/help#references in future posts.

          Comment


          • #6
            Roberto Iglesia Thank you for the feed back,
            Best regards,

            Marcos

            Comment

            Working...
            X