Announcement

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

  • Drop observations before certain event

    Hello,

    I have this dataset. I want to drop any observations before the first occurrence date (i.e., if medication X's first shortage happened in June 2017, any observation I have before June 2017 for X has to be drooped). Any suggestion?

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str16 NDC float change strL Generic long date float(shortage first_occure)
    "00002-7597-01" 0 "Olanzapine Injection" 664 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 665 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 666 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 667 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 668 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 669 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 670 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 671 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 672 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 673 0 674
    "00002-7597-01" 1 "Olanzapine Injection" 674 1 674
    "00002-7597-01" 0 "Olanzapine Injection" 675 1 674
    "00002-7597-01" 0 "Olanzapine Injection" 676 1 674
    "00002-7597-01" 0 "Olanzapine Injection" 677 1 674
    "00002-7597-01" 0 "Olanzapine Injection" 678 1 674
    "00002-7597-01" 0 "Olanzapine Injection" 679 1 674
    "00002-7597-01" 0 "Olanzapine Injection" 680 1 674
    "00002-7597-01" 0 "Olanzapine Injection" 681 1 674
    "00002-7597-01" 0 "Olanzapine Injection" 682 1 674
    "00002-7597-01" 0 "Olanzapine Injection" 683 1 674
    "00002-7597-01" 1 "Olanzapine Injection" 684 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 685 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 686 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 687 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 688 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 689 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 690 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 691 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 692 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 693 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 694 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 695 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 696 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 697 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 698 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 699 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 700 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 701 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 702 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 703 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 704 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 705 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 706 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 707 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 708 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 709 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 710 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 711 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 712 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 713 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 714 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 715 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 716 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 717 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 718 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 719 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 720 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 721 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 722 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 723 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 724 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 725 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 726 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 727 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 728 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 729 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 730 0 674
    "00002-7597-01" 0 "Olanzapine Injection" 731 0 674
    "00003-0371-13" 0 "Belatacept Injection" 664 0 686
    "00003-0371-13" 0 "Belatacept Injection" 665 0 686
    "00003-0371-13" 0 "Belatacept Injection" 666 0 686
    "00003-0371-13" 0 "Belatacept Injection" 667 0 686
    "00003-0371-13" 0 "Belatacept Injection" 668 0 686
    "00003-0371-13" 0 "Belatacept Injection" 669 0 686
    "00003-0371-13" 0 "Belatacept Injection" 670 0 686
    "00003-0371-13" 0 "Belatacept Injection" 671 0 686
    "00003-0371-13" 0 "Belatacept Injection" 672 0 686
    "00003-0371-13" 0 "Belatacept Injection" 673 0 686
    "00003-0371-13" 0 "Belatacept Injection" 674 0 686
    "00003-0371-13" 0 "Belatacept Injection" 675 0 686
    "00003-0371-13" 0 "Belatacept Injection" 676 0 686
    "00003-0371-13" 0 "Belatacept Injection" 677 0 686
    "00003-0371-13" 0 "Belatacept Injection" 678 0 686
    "00003-0371-13" 0 "Belatacept Injection" 679 0 686
    "00003-0371-13" 0 "Belatacept Injection" 680 0 686
    "00003-0371-13" 0 "Belatacept Injection" 681 0 686
    "00003-0371-13" 0 "Belatacept Injection" 682 0 686
    "00003-0371-13" 0 "Belatacept Injection" 683 0 686
    "00003-0371-13" 0 "Belatacept Injection" 684 0 686
    "00003-0371-13" 0 "Belatacept Injection" 685 0 686
    "00003-0371-13" 1 "Belatacept Injection" 686 1 686
    "00003-0371-13" 0 "Belatacept Injection" 687 1 686
    "00003-0371-13" 0 "Belatacept Injection" 688 1 686
    "00003-0371-13" 0 "Belatacept Injection" 689 1 686
    "00003-0371-13" 0 "Belatacept Injection" 690 1 686
    "00003-0371-13" 0 "Belatacept Injection" 691 1 686
    "00003-0371-13" 0 "Belatacept Injection" 692 1 686
    "00003-0371-13" 0 "Belatacept Injection" 693 1 686
    "00003-0371-13" 0 "Belatacept Injection" 694 1 686
    "00003-0371-13" 0 "Belatacept Injection" 695 1 686
    end
    format %tmMonth_CCYY date
    format %tmMonth_CCYY first_occure
    label values date Lama


  • #2
    assuming that your variable "date" is the relevant one:
    Code:
    drop if date<tm(2017m1)
    Last edited by Rich Goldstein; 30 May 2022, 12:09.

    Comment


    • #3
      Thank you, Rich. I was looking for one command that can drop all the before-occurrence observations because I have +200,000 observations with different dates.

      Comment


      • #4
        If this were my problem, I would do

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str16 NDC float change strL Generic long date float(shortage first_occure)
        "00002-7597-01" 0 "Olanzapine Injection" 664 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 665 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 666 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 667 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 668 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 669 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 670 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 671 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 672 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 673 0 674
        "00002-7597-01" 1 "Olanzapine Injection" 674 1 674
        "00002-7597-01" 0 "Olanzapine Injection" 675 1 674
        "00002-7597-01" 0 "Olanzapine Injection" 676 1 674
        "00002-7597-01" 0 "Olanzapine Injection" 677 1 674
        "00002-7597-01" 0 "Olanzapine Injection" 678 1 674
        "00002-7597-01" 0 "Olanzapine Injection" 679 1 674
        "00002-7597-01" 0 "Olanzapine Injection" 680 1 674
        "00002-7597-01" 0 "Olanzapine Injection" 681 1 674
        "00002-7597-01" 0 "Olanzapine Injection" 682 1 674
        "00002-7597-01" 0 "Olanzapine Injection" 683 1 674
        "00002-7597-01" 1 "Olanzapine Injection" 684 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 685 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 686 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 687 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 688 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 689 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 690 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 691 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 692 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 693 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 694 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 695 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 696 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 697 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 698 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 699 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 700 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 701 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 702 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 703 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 704 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 705 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 706 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 707 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 708 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 709 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 710 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 711 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 712 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 713 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 714 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 715 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 716 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 717 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 718 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 719 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 720 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 721 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 722 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 723 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 724 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 725 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 726 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 727 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 728 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 729 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 730 0 674
        "00002-7597-01" 0 "Olanzapine Injection" 731 0 674
        "00003-0371-13" 0 "Belatacept Injection" 664 0 686
        "00003-0371-13" 0 "Belatacept Injection" 665 0 686
        "00003-0371-13" 0 "Belatacept Injection" 666 0 686
        "00003-0371-13" 0 "Belatacept Injection" 667 0 686
        "00003-0371-13" 0 "Belatacept Injection" 668 0 686
        "00003-0371-13" 0 "Belatacept Injection" 669 0 686
        "00003-0371-13" 0 "Belatacept Injection" 670 0 686
        "00003-0371-13" 0 "Belatacept Injection" 671 0 686
        "00003-0371-13" 0 "Belatacept Injection" 672 0 686
        "00003-0371-13" 0 "Belatacept Injection" 673 0 686
        "00003-0371-13" 0 "Belatacept Injection" 674 0 686
        "00003-0371-13" 0 "Belatacept Injection" 675 0 686
        "00003-0371-13" 0 "Belatacept Injection" 676 0 686
        "00003-0371-13" 0 "Belatacept Injection" 677 0 686
        "00003-0371-13" 0 "Belatacept Injection" 678 0 686
        "00003-0371-13" 0 "Belatacept Injection" 679 0 686
        "00003-0371-13" 0 "Belatacept Injection" 680 0 686
        "00003-0371-13" 0 "Belatacept Injection" 681 0 686
        "00003-0371-13" 0 "Belatacept Injection" 682 0 686
        "00003-0371-13" 0 "Belatacept Injection" 683 0 686
        "00003-0371-13" 0 "Belatacept Injection" 684 0 686
        "00003-0371-13" 0 "Belatacept Injection" 685 0 686
        "00003-0371-13" 1 "Belatacept Injection" 686 1 686
        "00003-0371-13" 0 "Belatacept Injection" 687 1 686
        "00003-0371-13" 0 "Belatacept Injection" 688 1 686
        "00003-0371-13" 0 "Belatacept Injection" 689 1 686
        "00003-0371-13" 0 "Belatacept Injection" 690 1 686
        "00003-0371-13" 0 "Belatacept Injection" 691 1 686
        "00003-0371-13" 0 "Belatacept Injection" 692 1 686
        "00003-0371-13" 0 "Belatacept Injection" 693 1 686
        "00003-0371-13" 0 "Belatacept Injection" 694 1 686
        "00003-0371-13" 0 "Belatacept Injection" 695 1 686
        end
        format %tmMonth_CCYY date
        format %tmMonth_CCYY first_occure
        label values date Lama
        cls
        egen id = group(NDC)
        
        drop first_occure
        
        tempvar first
        
        qbys id: egen `first' = min(cond(sho == 1, da, .))
        
        g rel = date-`first'
        
        drop if rel < 0
        And now you can drop observations that're below the threshold you've set.
        Last edited by Jared Greathouse; 30 May 2022, 14:52.

        Comment

        Working...
        X