Announcement

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

  • dropping observation for dates

    Dear Statalist users

    I am working on Pandemic Crisis. I need to drop all those firms/observations that have YEDate from 1 February 2020 to 31 May 2020 which belong to Data_Year 2019. I want to keep all those firms that have YEDate in these months for other periods, but not for Data_Year 2019.

    I am using
    Code:
    drop if Data_Year==2019 & YEDate=="2/1/2020"
    But it says
    Code:
    (0 observations deleted)
    even though there are observations with this YEDate.

    Can you please help me?

    Thanks
    Qazi

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 YEDate int Data_Year
    "  1/1/2019" 2018
    "  1/1/2021" 2020
    "  1/1/2022" 2021
    "  1/1/2022" 2021
    "  1/1/2022" 2021
    "  1/1/2022" 2021
    "  1/1/2022" 2021
    "  1/2/2020" 2019
    "  1/2/2021" 2020
    "  1/2/2021" 2020
    "  1/2/2021" 2020
    "  1/2/2022" 2021
    "  1/2/2022" 2021
    "  1/2/2022" 2021
    "  1/2/2022" 2021
    "  1/2/2022" 2021
    "  1/2/2022" 2021
    "  1/2/2022" 2021
    "  1/2/2022" 2021
    "  1/3/2019" 2018
    "  1/3/2020" 2019
    "  1/3/2021" 2020
    "  1/3/2021" 2020
    "  1/3/2021" 2020
    "  1/3/2021" 2020
    "  1/4/2019" 2018
    "  1/4/2020" 2019
    "  2/1/2020" 2019
    "  2/2/2019" 2018
    "  2/2/2020" 2019
    "  2/3/2018" 2017
    "  2/3/2019" 2018
    "  2/4/2018" 2017
    "  3/1/2018" 2017
    "  3/1/2020" 2019
    "  3/2/2019" 2018
    "  3/3/2018" 2017
    "  3/3/2021" 2020
    "  3/3/2022" 2021
    "  3/5/2022" 2021
    "  3/6/2021" 2020
    "  3/7/2020" 2019
    "  3/9/2019" 2018
    "  4/1/2018" 2017
    "  4/1/2018" 2017
    "  4/1/2019" 2018
    "  4/1/2019" 2018
    "  4/2/2018" 2017
    "  4/2/2018" 2017
    "  4/2/2021" 2020
    "  4/2/2022" 2021
    "  4/2/2022" 2021
    "  4/2/2022" 2021
    "  4/3/2020" 2019
    "  4/3/2020" 2019
    "  4/3/2021" 2020
    "  4/3/2021" 2020
    "  4/3/2021" 2020
    "  4/4/2021" 2020
    "  4/4/2021" 2020
    "  4/4/2021" 2020
    "  4/5/2020" 2019
    "  5/1/2021" 2020
    "  5/1/2021" 2020
    "  5/2/2020" 2019
    "  5/2/2020" 2019
    "  5/2/2021" 2020
    "  5/2/2021" 2020
    "  6/2/2019" 2018
    "  6/3/2018" 2017
    "  7/1/2017" 2017
    "  7/1/2017" 2017
    "  7/1/2017" 2017
    "  7/1/2017" 2017
    "  7/1/2017" 2017
    "  7/1/2018" 2018
    "  7/1/2018" 2018
    "  7/1/2018" 2018
    "  7/1/2021" 2021
    "  7/2/2017" 2017
    "  7/2/2020" 2020
    "  7/3/2021" 2021
    "  7/3/2021" 2021
    "  7/3/2021" 2021
    "  8/1/2020" 2020
    "  8/3/2018" 2018
    "  9/1/2018" 2018
    "  9/1/2018" 2018
    "  9/2/2017" 2017
    "  9/2/2017" 2017
    " 1/24/2021" 2020
    " 1/25/2020" 2019
    " 1/25/2020" 2019
    " 1/25/2020" 2019
    " 1/26/2019" 2018
    " 1/26/2019" 2018
    " 1/26/2019" 2018
    " 1/26/2019" 2018
    " 1/26/2020" 2019
    " 1/27/2018" 2017
    end

  • #2
    thank you for using -dataex- which allows one to see that your YEDate variable has leading spaces; so, if you use the following before your -drop- command you will find that there are observations that are dropped:
    Code:
    replace YEDate=strtrim(YEDate)

    Comment


    • #3
      It's because you have leading spaces, and those spaces count as a valid character. Try:

      Code:
      replace YEDate = trim(YEDate)
      drop if Data_Year==2019 & YEDate=="2/1/2020"
      However, more importantly, this is not an ideal way to code it because you will have to write one line for each date to be deleted. I'd suggest create a numerical date, and use logical condition to delete the cases:

      Code:
      * Alternative approach
      gen new_date = date(YEDate, "MDY")
      format new_date %td
      drop if Data_Year == 2019 & new_date > date("2/1/2020", "MDY") & new_date < date("5/31/2020", "MDY")

      Comment


      • #4
        Dear Rich and Ken

        Thank you so much. It worked like a charm.
        THANK YOU!

        Warmest regards

        Comment

        Working...
        X