Announcement

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

  • Deleting observations after a particular date.

    I want to delete all observations in a panel dataset after 2021-01 through 2021-03. Would you please tell me how I can delete them? Thank you.

    . tab date

    date | Freq. Percent Cum.
    ----------------------+-----------------------------------
    2017-01 | 2,430 1.97 1.97
    2017-02 | 2,430 1.97 3.95
    ………

    2020-11 | 2,430 1.97 92.75
    2020-12 | 2,430 1.97 94.73
    2021-01 | 2,165 1.76 96.48
    2021-02 | 2,165 1.76 98.24
    2021-03 | 2,165 1.76 100.00
    ----------------------+-----------------------------------
    Total | 123,135 100.00



  • #2
    Could you please show us your example dataset using command -dataex-? We won't be able to give precise suggestion unless we know the storage type of "date".

    Comment


    • #3
      Please see below for the variable information. I am not sure why "2021-02" and "2021-03" do not sure in the list below. Thank you for the help.

      ----------------------- copy starting from the next line -----------------------
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str21 date
      "2017-01"
      "2017-02"
      -----
      
      "2020-11"
      "2020-12"
      "2021-01"
      end
      ------------------ copy up to and including the previous line ------------------

      Listed 100 out of 123135 observations
      Use the count() option to list more


      Comment


      • #4
        Your dates are clear but not yet fit for any serious Stata purpose. I am alarmed on your behalf that date is shown as str21 which suggests that garbage has been imported in some observations.

        Code:
        list date if strlen(date) > 7
        will flush out unsuitable values. Otherwise this shows some technique


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str21 date
        "2017-01"
        "2017-02"
        "2020-11"
        "2020-12"
        "2021-01"
        end
        
        gen mdate = monthly(date, "YM")
        format mdate %tm 
        
        list
        Now you can just

        Code:
        drop if mdate >= ym(2021, 1)

        Comment

        Working...
        X