Announcement

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

  • Leave out 90th percentile

    I am using a census dataset which has these following variables:
    county industry income
    I know how to calculate the normal one: collapse (p90) income, by(county industry).

    How do I calculate a leave-out 90th percentile income by industry? Suppose there are 3 counties (i=1,2,3) and 3 industries (j=1,2,3). For county i=1 and industry j=1, I would like to get the 90th percentile income of people in industry j=1 and in counties i=2 and 3.

    Thanks a lot in advance!





  • #2
    This will do it:
    Code:
    //  CREATE TOY DATA SET
    clear*
    set obs 2
    gen country = _n
    expand 3
    by country, sort: gen industry = _n
    expand 200
    by country industry, sort: gen firm = _n
    
    set seed 1234
    gen x = runiform()
    
    
    //  SOLUTION STARTS HERE
    gen p90_leave_out_same_industry = .
    levelsof country, local(countries)
    foreach c of local countries {
        levelsof industry if country == `c', local(industries)
        foreach i of local industries {
            quietly summ x if country == `c' & industry != `i', detail
            replace p90_leave_out_same_industry = `r(p90)' if country == `c' ///
                & industry == `i'
        }
    }
    Next time you ask for help with code, please provide example data, and use the -dataex- command to do that.

    If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      You may also like to use asrol (from SSC) here. When finding percentiles using asrol, we have to use the stat(median) and perc() options together. Using Clyde Schechter example data, here is the asrol code
      Code:
      ssc install asrol
      
      bys country : asrol x, stat(median) perc(.9) xf(industry) gen(p90xf)
      
      * Deleting observation just to show unique values by country and industry
      
      duplicates drop country industry, force
      
      list country industry p90_leave_out_same_industry p90xf
      
           +-------------------------------------------+
           | country   industry   p90_le~y       p90xf |
           |-------------------------------------------|
        1. |       1          1   .8961145   .89611453 |
        2. |       1          2   .8890606   .88906059 |
        3. |       1          3   .9138606   .91386062 |
        4. |       2          1   .8997788   .89977887 |
        5. |       2          2   .9169899   .91698989 |
           |-------------------------------------------|
        6. |       2          3   .9045044   .90450439 |
           +-------------------------------------------+
      You can find more on asrol here https://fintechprofessor.com/asrol-f...tics-in-stata/
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment


      • #4
        Thanks a lot, Clyde and Attaullah!

        Comment

        Working...
        X