Announcement

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

  • Remove the hour of a date + hour variable

    Hi All,

    I have a date variable that looks like this:

    01apr2014 17:06:55

    The storage type for this variable is double and display format %tc

    I just need the date, that is 01apr2014. How can I delete the time.

    Thank you,
    Marvin


  • #2
    All documented at help dates and times. The function dofc() achieves this.

    Code:
    . di %18.0f clock("01apr2014 17:06:55", "DMY hms")
         1711991215000
    
    . di %tc clock("01apr2014 17:06:55", "DMY hms")
    01apr2014 17:06:55
    
    . di %td dofc(clock("01apr2014 17:06:55", "DMY hms"))
    01apr2014
    The FAQ Advice advises:

    3. Before you post

    Before posting, consider other ways of finding information:
    • the online help for Stata

    Comment


    • #3
      Hi Nick,

      I am sorry about this but although I tried to find this answer in the internet I could figure this out. Again I am sorry but I did not understand your commands. Perhaps I did not explain my post well. I just need to format my date variable so I only see the date (not the time), that is 01apr2014. Or create a new variable with only the date.

      Basically what I need to do is to delete all the observations after 21apr2015 14:31:18. I tried the following

      keep if SystemDate > td("24apr2015 13:09:53")

      But it didn't work. Just to reiterate, the storate type is double for this variable.
      Last edited by Marvin Aliaga; 27 Apr 2015, 13:51.

      Comment


      • #4
        There really is no need to try to find answers to questions on the internet when Stata itself contains the documentation that explains what you need. That's like going to a library when you already have the answer in a book on your desk.

        create a new variable with only the (daily) date

        This is what dofc() does, which I referred you to, so all you need to do is use it in a generate command:

        Code:
        gen dailydate = dofc(datetime)
        format my date variable so I only see the date (not the time)

        The help I referred you contains a link to

        Code:
        help datetime_display_formats
        from which it follows that an answer could be

        Code:
        format datetime %tcD_M_CY
        although there are many other answers, as there are many formats for showing daily dates.

        delete all the observations after 21apr2015 14:31:18

        I don't understand the code you give, as you use a quite different time, and you use keep where drop is needed:

        Code:
        * Marvin's code
        keep if SystemDate > td("24apr2015 13:09:53")
        td() is documented, but as a function for mapping daily dates in human-readable form to daily dates in Stata-readable form. I think the question in bold is solved by

        Code:
        drop if SystemDate >  tc("21apr2015 14:31:18")


        Last edited by Nick Cox; 27 Apr 2015, 15:09.

        Comment


        • #5
          Thank you so much!!!!

          Comment


          • #6
            Hi Nick,

            I am sorry I come back with this after all this time.

            I have this date variable SystemDate (22jan2015 11:46:02 )

            storage type: double
            Display format: %tc

            I just need the date without the time. This is my try:

            gen SystemDateRC = dofc( SystemDate )
            format SystemDateRC %tcD_M_CY

            But the dates between my old and new date variables don't match.

            22jan2015 15:25:59 = 01 January 1960

            Do you know what I am doing wrong?

            Thank you,
            Marvin




            Comment


            • #7
              Wrong format completely. A daily date needs a daily date format.

              Comment


              • #8
                Completely was too strong a word: format should begin %td, however, not %tc

                Comment


                • #9
                  Hi Nick,
                  I want to delete time in a variable where the date and time are combined like that var 25/06/2016 12:01:21.000000000 to 25/06/2016?
                  Thank you
                  Sabrine

                  Comment


                  • #10
                    Sabrina: Your question is already answered in #2. Use dofc() and format %td

                    Comment


                    • #11
                      Thank for your request, I have already do that but no change
                      may be because there are lot off zero in time variable (12:01:21.000000000)

                      Comment


                      • #12
                        Sabrina, if your timestamp is stored as string, dofc() won't work: it requires a numeric value. You have to convert your string to a date or a clock value (both are numeric).

                        Here is an example:

                        Code:
                        clear
                        set obs 1
                        gen time="25/06/2016 12:01:21.000000000"
                        * Convert to clock then to date
                        gen c=clock(time,"DMYhms#")
                        gen d1=dofc(c)
                        * Convert directly to date
                        gen d2=date(time,"DMY#")
                        format %tc c
                        format %td d*
                        list
                        Code:
                             +----------------------------------------------------------------------------+
                             |                          time                    c          d1          d2 |
                             |----------------------------------------------------------------------------|
                          1. | 25/06/2016 12:01:21.000000000   25jun2016 12:01:21   25jun2016   25jun2016 |
                             +----------------------------------------------------------------------------+
                        See also https://www.stata.com/help.cgi?datetime_translation

                        Hope this helps,

                        Jean-Claude Arbaut
                        Last edited by Jean-Claude Arbaut; 23 Apr 2018, 14:14.

                        Comment


                        • #13
                          Thank you Jean Claude for your help

                          Comment


                          • #14
                            I want to generate a variable = 1 if the date = 01jun16 the code
                            generate j1=0
                            replace j1=1 if date==("01jun16","DMY")
                            but the code not work

                            Comment


                            • #15
                              instead of your command
                              Code:
                              replace j1=1 if date==("01jun16","DMY")
                              try:
                              Code:
                              replace j1=1 if date==date("01jun2016","DMY")
                              note that I have guessed the century - replace the "20" with whatever you want if I am wrong

                              Comment

                              Working...
                              X