Announcement

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

  • Create a simple grouping variable

    Hello

    I'm sure this is something simple but how do you create a grouping variable (say "Group") where Group=1 for for the first 10 observations, Group=2 for the next 10 and so on without writing
    Code:
    gen Group=1 if _n<=10
    then
    Code:
    replace Group=2 if Group==. &_n<=20
    then
    Code:
    replace Group=3 if Group=. & _n<=30
    etc.etc.

    or a loop of some sort.

    Regards

    Chris

  • #2

    Code:
    gen group = ceil(_n/10) 
    
    egen group = seq(), block(10)

    Note that even with #1 you can simplify to


    Code:
     
     gen Group=1 in 1/10  
     replace Group=2 in 11/20   
     replace Group=3 in 21/30
    I am not recommending that, but it's less typing than #1 (and a little more efficient too).

    Comment


    • #3
      Thanks Nick.
      Thought there would be an efficient way like that but i just couldn't find it.
      Regards
      Chris

      Comment

      Working...
      X