Announcement

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

  • egen by groups

    Dear Statalist, happy new year!

    I want egen to create 5 class of incomes for each country in my dataset.

    I tried with this, but it doesn't work:

    Code:
    bysort country: egen income_class = cut(income), group(5)
    it says egen ... cut() may not be combined with by (sigh...)

    Is there a fast way to do this?

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(s4 s7)
    1    8000
    1   65000
    1    2500
    1   60000
    1   60000
    1   80000
    1   56000
    1   45000
    1   55000
    1   80000
    1   42000
    1   90000
    1   70000
    1   60000
    1  140000
    1   53000
    1   20000
    1   30000
    1    8000
    1   10000
    1   15000
    1   50000
    1  130000
    1   24000
    1   40000
    1   60000
    1  100000
    1  120000
    1   70000
    1  100000
    1   40000
    1   15000
    1   70000
    1    4300
    1  100000
    1  120000
    1   60000
    1  120000
    1   38000
    1   35000
    1  130000
    1   35000
    1   30000
    1  100000
    1  140000
    1   70000
    1   50000
    1   55000
    1   65000
    1   96000
    1   30000
    1   49000
    1   28000
    1   80000
    1   40000
    1   75000
    1   30000
    1  100000
    1   13000
    1   48000
    1   56700
    1   30000
    1    3800
    1   30000
    1   52000
    1   40000
    1   75000
    1   54000
    1    3500
    1   55000
    1   70600
    1   75000
    1   70000
    1   50000
    1   40000
    1   70000
    2   90000
    2   15000
    2   11000
    2   43000
    2   80000
    2   38000
    2 1000000
    2   40000
    2   48000
    2   28000
    2  110000
    2   70000
    2   50000
    2  120000
    2   60000
    2   36000
    2   16000
    2   16000
    2   70000
    2    5000
    2   45000
    2   48000
    2   80000
    2  125000
    end
    label values s4 s4
    label def s4 1 "Austria", modify
    label def s4 2 "Fantasyland", modify
    ok
    Last edited by Martinо Cоmelli; 30 Dec 2022, 08:05.

  • #2
    The help for egen makes clear whenever by: is supported for a particular function, and here there is no such support.

    There is a community-contributed function xtile() within egenmore from SSC.

    Code:
    ssc install egenmore 
    
    help egenmore

    Comment


    • #3
      Nick Cox also on December 30, you know the best tips and tricks, thank you SO much for your service to the stata community (especially from the most clueless members like myself);
      happy new year!!!!

      Comment


      • #4
        The user written -egen, xtile()- from egenmore is the classic in the genre. But it is very slow. It does the job perfectly fine for moderately large datasets.

        The user written byable xtiles from the gtools package are fast as lightning. One might want to revert to gtools if dealing with large datasets.

        Comment

        Working...
        X