Announcement

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

  • group two variables by year

    Hello all,

    I want to create a gourp based on size and beta and update it once a year.

    I tried to use
    Code:
    bys year: egen id = group(size_quin beta_decile)
    However group() can't be combined with by. Is there a way that I can generate an id by size and beta on a yearly basis?

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(firm year) float(date size_quin) double beta_decile
    10001 2015 660 1 2
    10001 2008 581 1 1
    10001 2008 584 1 1
    10001 2012 625 1 2
    10001 2009 588 1 4
    10001 2008 586 1 1
    10001 2014 656 1 1
    10001 2013 646 1 2
    10001 2011 623 1 2
    10001 2007 573 1 4
    10001 2016 681 1 1
    10001 2016 678 1 1
    10001 2016 676 1 1
    10001 2014 649 1 1
    10001 2007 575 1 4
    10001 2015 667 1 2
    10001 2007 571 1 4
    10001 2011 622 1 2
    10001 2011 614 1 2
    10001 2009 599 1 4
    10001 2007 567 1 4
    10001 2015 670 1 2
    10001 2013 645 1 2
    10001 2011 618 1 2
    10001 2014 655 1 1
    10001 2013 643 1 2
    10001 2014 658 1 1
    10001 2011 615 1 2
    10001 2014 650 1 1
    10001 2016 680 1 1
    10001 2007 570 1 4
    10001 2015 663 1 2
    10001 2016 677 1 1
    10001 2007 566 1 4
    10001 2009 589 1 4
    10001 2010 600 1 2
    10001 2008 576 1 1
    10001 2013 642 1 2
    10001 2016 673 1 1
    10001 2015 671 1 2
    10001 2013 644 1 2
    10001 2008 577 1 1
    10001 2014 659 1 1
    10001 2013 647 1 2
    10001 2010 606 1 2
    10001 2008 582 1 1
    10001 2012 632 1 2
    10001 2015 666 1 2
    10001 2010 605 1 2
    10001 2016 672 1 1
    10001 2012 633 1 2
    10001 2013 639 1 2
    10001 2013 636 1 2
    10001 2014 652 1 1
    10001 2016 674 1 1
    10001 2014 654 1 1
    10001 2012 631 1 2
    10001 2015 668 1 2
    10001 2012 626 1 2
    10001 2011 620 1 2
    10001 2012 630 1 2
    10001 2014 648 1 1
    10001 2016 683 1 1
    10001 2011 621 1 2
    10001 2013 641 1 2
    10001 2008 583 1 1
    10001 2011 619 1 2
    10001 2012 624 1 2
    10001 2007 569 1 4
    10001 2010 603 1 2
    10001 2009 592 1 4
    10001 2012 634 1 2
    10001 2009 591 1 4
    10001 2007 564 1 4
    10001 2010 604 1 2
    10001 2009 594 1 4
    10001 2015 669 1 2
    10001 2013 637 1 2
    10001 2010 611 1 2
    10001 2007 574 1 4
    10001 2007 572 1 4
    10001 2012 629 1 2
    10001 2010 608 1 2
    10001 2015 664 1 2
    10001 2008 587 1 1
    10001 2011 613 1 2
    10001 2010 609 1 2
    10001 2008 580 1 1
    10001 2010 610 1 2
    10001 2012 628 1 2
    10001 2014 657 1 1
    10001 2011 616 1 2
    10001 2008 578 1 1
    10001 2009 598 1 4
    10001 2016 675 1 1
    10001 2009 593 1 4
    10001 2007 568 1 4
    10001 2009 596 1 4
    10001 2012 627 1 2
    10001 2010 607 1 2
    end
    format %tm date



  • #2

    Code:
     
     egen id = group(year size_quin beta_decile)
    is perfectly legal. If you wish groups to be numbered 1 up in each year, then that's just a matter of going back to first principles:

    Code:
    bysort year size_quin beta_decile : gen byte group = _n == 1 
    by year: replace group = sum(group)

    Comment


    • #3
      Thank you Nick, this is much nicer than the solution I just found.
      I used
      Code:
      forv i = 2000 /2018 {
          egen id`i' = group (size_quin beta_decile) if year == `i'
          }
          egen id = rowtotal(id*)

      Comment

      Working...
      X