Announcement

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

  • forvalues + pctile

    I am trying to compute 99th percentile value across distinct values of a given variable. Basically, i am trying to generate a variable "p99_depth" which shows the 99th percentile value for values of depth corresponding to each individual instrument. My knowledge of STATA is fairly limited and this is the code i came up with:

    //i generated a new variable 'group' to number each distinct value across my variable of interest (instrument):

    egen group = group(instrument)
    su group, meanonly
    forvalues i = 1/`r(max)' {
    gen p99_depth = pctile(depth), p(99) if group == i
    }

    This is giving me invalid syntax error. Any help fixing the code or an alternative approach to solving this problem would be appreciated. Thanks

  • #2
    Four errors that I can see:

    1. Should be egen not gen.

    2. The if qualifier is in the wrong place.

    3. The local macro lacks quotation marks.

    4. Second time round the loop, p99_depth already exists and the loop would fail.

    But as pctile() can be coupled with by: you don't need a loop at all.

    Code:
    bysort instrument: egen p99_depth = pctile(depth), p(99)

    Comment


    • #3
      that worked, thanks a lot!!

      Comment

      Working...
      X