Announcement

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

  • Dummy variable doesn't work, what am I doing wrong?

    Hello,

    I want to generate a dummy variable that gives a value of 1 if the value of a variable is between the range of the 5th and 95th percentile, 0 otherwise


    I did the following:

    Code:
    gen X_inrange = inrange(X, r(p5), r(p95))
    But afterwards for all firm-year observations it is giving an 1... I do not observe any 0s.. It gives a 1 for every observation no matter what...


    What am I doing wrong?
    Last edited by Jonathan Smits; 04 May 2017, 07:34.

  • #2
    Jonthan:
    you may want to try:
    Code:
    . set obs 1000
    
    . g X=runiform()+1000
    
    . centile X, centile(5 95)
    
                                                           -- Binom. Interp. --
        Variable |       Obs  Percentile    Centile        [95% Conf. Interval]
    -------------+-------------------------------------------------------------
               X |     1,000          5    1000.037        1000.027    1000.046
                 |                   95     1000.94        1000.931    1000.957
    
    . return list
    
    scalars:
                 r(n_cent) =  2
                      r(N) =  1000
                   r(ub_2) =  1000.957302458706
                   r(lb_2) =  1000.930621496683
                    r(c_2) =  1000.939984130859
                   r(ub_1) =  1000.045955191923
                   r(lb_1) =  1000.027411419193
                    r(c_1) =  1000.036810302734
    
    macros:
               r(centiles) : "5 95"
    
    . gen X_inrange = inrange(X, r(c_1), r(c_2))
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      It's only going to work while those results remain visible. Note that @Carlo applies generate immediately after the quantile calculation.


      Otherwise Stata substitutes missings and that means your calculation will always return 1 for true:


      Code:
      . di inrange(-1e9, ., .)
      1
      
      . di inrange(1e9, ., .)
      1

      Comment


      • #4
        Thanks guys!

        Comment

        Working...
        X