Announcement

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

  • Keep data from a specific date to 5 days after or calculate a new date from a specific date to 5 days after

    Hi,
    I have two questions (Q) on the example of my data shown below. The main dataset contains daily data of AskPrice in date1 for 1000 companies. ISIN is the indicator of each company. Source_date_2016 is a date of an event which occurred only once in a period of date1 for each company but, it is repeated because I have daily data for other variables.

    Q1- Since I merged two datasets together I lost the sort of date1 for each company, as you see date1 in the example. I wonder how can I sort date1 again for each company.

    Q2- Considering the range of date1, I want to keep the data started from Source_date_2016 to 5 working days after Source_date_2016 for each company.
    My incomplete code is: keep if inrange(date,Source_date_2015, 5 days)) but I don't know how to fit 5 days in the code. I am not sure either if this is a right code to use considering my query.

    I would be grateful if one could help me to find answers to my questions.

    Kind regards,
    Mahmoud

    Example:
    input str12 ISIN float date1 double AskPrice int Source_date_2016
    "AT00000AMAG3" 20454 . 20878
    "AT00000AMAG3" 20467 30.5 20878
    "AT00000AMAG3" 20577 30.81 20878
    "AT00000AMAG3" 20524 30.12 20878
    "AT00000AMAG3" 20466 30.785 20878
    "AT00000AMAG3" 20503 27.075 20878
    "AT00000AMAG3" 20594 31.45 20878
    "AT00000AMAG3" 20621 30.9 20878
    "AT00000AMAG3" 20507 26.98 20878
    "AT00000AMAG3" 20495 26.795 20878
    "AT00000AMAG3" 20612 31.2 20878
    "AT00000AMAG3" 20573 32.1 20878
    "AT00000AMAG3" 20457 31.68 20878
    "AT00000AMAG3" 20604 31.44 20878
    "AT00000AMAG3" 20514 29.98 20878
    "AT00000AMAG3" 20628 31.465 20878
    "AT00000AMAG3" 20551 31.5 20878
    "AT00000AMAG3" 20506 27.4 20878
    "AT00000AMAG3" 20633 30 20878
    "AT00000AMAG3" 20528 30.5 20878
    "AT00000AMAG3" 20488 28.25 20878
    "AT00000AMAG3" 20607 31.2 20878
    "AT00000AMAG3" 20543 31.22 20878
    "AT00000AMAG3" 20537 31.255 20878
    "AT00000AMAG3" 20635 29.975 20878
    "AT00000AMAG3" 20517 29.995 20878
    end
    format %tdNN/DD/CCYY date1
    format %tdnn/dd/CCYY Source_date_2016

  • #2
    What I understand you want is to keep observations that have Source_date_2016 being less than or equal to the lowest value of Source_date_2016 for each company. If thats the case, use below code. If not, please explain a bit better.

    First 5 days of Source_date_2016
    Code:
    bysort ISIN (Source_date_2016): egen startdate=min(Source_date_2016)
    keep if Source_date_2016-startdate<=5
    sort ISIN date1

    First 5 working days of Source_date_2016
    Code:
    ssc install workdays
    bysort ISIN (Source_date_2016): egen startdate=min(Source_date_2016)
    workdays startdate Source_date_2016, gen(No_of_workdays_passed)
    keep if No_of_workdays_passed<=5
    sort ISIN date1
    Edit:
    Actually, maybe you asked for keeping 5 days of data in terms of date1, relative to Source_date_2016. If that is the case your example is confusing as all date1 observations are before the Source_date_2016.

    If thats what you want, you can do:
    Code:
    ssc install workdays
    workdays Source_date_2016 date1, gen(No_of_workdays_passed)
    keep if inrange(No_of_workdays_passed, 0, 5)
    sort ISIN date1
    Last edited by Jorrit Gosens; 07 Aug 2018, 05:59.

    Comment


    • #3
      Hi Jorrit,
      Thnks for the kind help.
      Perhaps, I did not well ask my question. I simply want to create a new variable named for instance "date2" which is equal to the value of Source_date_2016 + 5 working days. For example, AT00000AMAG3 02/28/2016 + 5 working day = 03/07/2016. The date 03/07/2016 is the new variable named date2.

      Comment


      • #4
        Code:
        gen tempdate1=Source_date_2016+5
        gen tempdate2=Source_date_2016+6
        gen tempdate3=Source_date_2016+7
        gen tempdate4=Source_date_2016+8
        
        workdays Source_date_2016 tempdate1, gen(wkdays1)
        workdays Source_date_2016 tempdate2, gen(wkdays2)
        workdays Source_date_2016 tempdate3, gen(wkdays3)
        workdays Source_date_2016 tempdate4, gen(wkdays4)
        
        gen date2=tempdate1 if wkdays1==5
        replace date2=tempdate2 if wkdays2==5
        replace date2=tempdate3 if wkdays3==5
        replace date2=tempdate4 if wkdays4==5
        
        format %tdnn/dd/CCYY date2
        
        drop tempdate1 tempdate2 tempdate3 tempdate4 wkdays1 wkdays2 wkdays3 wkdays4

        Comment


        • #5
          Thanks Jorrit! It works perfectly.

          Would it be also possible to compute a new variable date3 which equals to Source_date_2016 + one month?

          Thanks in advance!

          Comment


          • #6
            Yes, but it depends on how you define one month.
            Should date1=5th of April mean date3=5th of May?
            What about when date1=31st of January? 31st of February is not a valid day.

            Or should it just be date3=date1+30 days in all cases?
            Should the variable date3 only take values of working days?

            Specify the rules for date3 and someone will be able to answer

            Comment


            • #7
              date1=5th of April should mean date3=5th of May

              Comment


              • #8
                https://www.ssc.wisc.edu/sscc/pubs/stata_dates.htm
                See the section Months and Years

                Comment


                • #9
                  Thanks for the link. I appreciate it.

                  Comment

                  Working...
                  X