Announcement

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

  • Question about writing a programme that takes on different values based on the percentile

    I am supposed to write a programme called vx, that takes on the value of 1 if the variable’s value is in the top 5th percentile and 0 otherwise and error if input variable is categorical variable.

    capture program drop vx
    program vx
    syntax varname
    su `varlist'
    if var == factor {
    display "Invalid argument: Input variable is not a categorical variable"
    }
    else var >= `r(p95)' {
    display "1"
    }
    else var < `r(p95)' {
    display "0"
    }
    }
    end

    I ran the programme on 3 variables: length, mass and weight. I ended up with the error 111, which said var could not be found. Is there a way to remedy the programme?
    Last edited by Goh jedrek; 03 Jun 2021, 07:01.

  • #2
    Is this an assignment? See our guidance at FAQ Advice Extras.

    The central technique you presumably need is

    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . summarize price, detail
    
                                Price
    -------------------------------------------------------------
          Percentiles      Smallest
     1%         3291           3291
     5%         3748           3299
    10%         3895           3667       Obs                  74
    25%         4195           3748       Sum of wgt.          74
    
    50%       5006.5                      Mean           6165.257
                            Largest       Std. dev.      2949.496
    75%         6342          13466
    90%        11385          13594       Variance        8699526
    95%        13466          14500       Skewness       1.653434
    99%        15906          15906       Kurtosis       4.819188
    
    . gen wanted = price &gt;= r(p95) if price &lt; .
    
    . tab wanted
    
         wanted |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              0 |         70       94.59       94.59
              1 |          4        5.41      100.00
    ------------+-----------------------------------
          Total |         74      100.00

    The evident problem with your program is that Stata has no idea what you mean by var, as it's not a variable or scalar in the dataset. The code also seems confused between looking at an entire variable and looking at individual values.

    Comment

    Working...
    X