Announcement

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

  • Dividing panel by quartiles

    Hello everyone,

    I am dividing my panel data into 4 quartiles by country's GDP and year. A country may or may not be in the same quartile each year. But, for most part they will belong to same quartile. This dataset has 130 countries and 33 periods starting from 1984 to 2016.
    For this, I run the following code:
    Code:
     sort cty year
    
    . forval x = 25(25) 75 {
      2. by year : egen p`x' = pctile(rgdpna), p(`x')
      3. }
    rgdpna is the real GDP of a country. cty = country

    However, I get an error message.
    not sorted
    r(5);
    .

    I tried including -sort- command in the -for- loop but didn't work either.

    Could you tell me where am I going wrong?

  • #2
    Try
    Code:
    sort year cty
    David Radwin
    Senior Researcher, California Competes
    californiacompetes.org
    Pronouns: He/Him

    Comment


    • #3
      David Radwin Thank you replying. I tried. But, there was another error message.

      Code:
       sort  year cty
      
      . forval x = 25(25) 75 {
        2. by year : egen p`x' = pctile(rgdpna), p(`x')
        3. }
      variable p25 already defined
      r(110);

      Comment


      • #4
        You'll need to drop that variable (p25) before you generate it again. Same for other variables that may exist. Here is a shortcut:

        Code:
        forval x = 25(25) 75 {
             capture drop p`x'
             by year : egen p`x' = pctile(rgdpna), p(`x')
             }
        David Radwin
        Senior Researcher, California Competes
        californiacompetes.org
        Pronouns: He/Him

        Comment


        • #5
          Note that using the xtile function for egen in egenmore (SSC) is another way to get quartile bins as you evidently want.

          Comment


          • #6
            David Radwin and Nick Cox Thank you both those suggestions worked for my dataset.

            Comment

            Working...
            X