Announcement

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

  • xtile and egen

    I want to categorize a variable on the basis of any set of cutpoints, not just percentiles. At the same time, since I have a panel, I want to do it for each point of time separately. In the following code and data, I categorize my firms on the basis of percentiles but when I use cutpoints(cpt) instead of nq() , it shows this error to me: option cutpoints() not allowed.

    How can I categorize my firms in terms of cpt at each point of time? (Preferably, without using loops, because my data set is large)




    Code:
    * sample generated data
    clear all
    input time firm value cpt
        
    1        1        3        4    
    1        2        4        8    
    1        3        5        10    
    1        4        6        .    
    1        5        7        .
    1        6        8        .
    1        7        9        .
    1        8        10        .
    1        9        11        .
    2        1        4        6    
    2        2        6        10    
    2        3        7        12    
    2        4        9        .
    2        5        12        .
    2        6        13        .
    2        7        15        .
    end
    
    * finding quartiles for each time
    egen perc = xtile(value), by(time) nq(4)
    Last edited by Mohammad Khodadadi; 22 May 2017, 14:51.

  • #2
    You are confusing the official Stata command -xtile- with the -egen- function -xtile()- (from the -egenmore- package). The former accepts a -cutpoints()- option, but the latter does not. Unfortunately, you also want a -by()- option, which the latter supports and the former does not! See -help xtile- and -help egenmore-.

    So you need to do this in a loop over time:

    Code:
    foreach t of local times {
        gen current_cpt = (cond(time == `t'), cpt, .)
        xtile group_perc = value if time == `t', cutpoints(current_cpt)
        replace perc = group_perc if time == `t'
        drop group_perc current_cpt
    }

    Comment


    • #3
      Dear Clyde,

      Thanks a lot for your help. :-)

      Mohammad

      Comment

      Working...
      X