Announcement

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

  • remove time from clock format

    Hi all

    Very new to stata but perhaps this is quite advanced.

    I'm looking to remove time and show only date in my stata variable registrationdate. currently it is showing both date and time.

    my goal is to be able to work out date difference between when something was registered and todays date.

    however first i need to remove time from the registrationdate variable which is currently in following format

    %tCMonth_dd,_CCYY

    Have researched this for last few hours and got no where unfortunately

    Any help appreciated

  • #2
    So, we need to distinguish between two things: reformatting the variable to only display the date part vs actually extracting the date into a new date variable that does not include time. For the purposes of calculating the elapsed number of days between two such variables, the former is useless and the latter is right on point.

    Code:
    gen wanted = datediff(dofC(registrationdate), daily("`c(current_date)'", "DMY", "d"))
    will give you the number of days between the date of registration and today's date. If you would like this duration denominated in, say, months, or years, change "d" in the third argument to "m" or "y", respectively. Actually, if you want it in months or years, it is more likely you are interested in fractions of a month or year as well, so you would be better off using -datediff_frac()- instead of -datediff()-.

    Added: It is a truism that it is best to use variable names that describe what the variable represents. I think with date and time variables we can go farther. Given that date-time variables are handled rather differently from date variables in Stata, and trying to use one where the other is required will lead to nonsensical results, I think it is important not to use "date" in the name of a datetime variable. I usually name variables with _date when they are truly date variables, and _datetime, _timestamp, or _dttm when they contain both date and time.
    Last edited by Clyde Schechter; 22 Jun 2023, 16:01.

    Comment


    • #3
      thanks for explaining Clyde

      i follow what you have done there. turns out i dont need to remove the time data at all

      when i copy that code in stata it is saying type mismatch for some reason however

      Comment


      • #4
        The only possibility for a type mismatch in that command is if your variable registrationdate is actually a string, not a real Stata datetime (clock) variable. But that isn't compatible with your statement that it is formatted %tCMonth_dd,_CCYY.

        So, the only way to resolve this is for you to show example data from your Stata dataset using the -dataex- command. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

        Edit: I take that back. The code I showed is mistyped and has parentheses in the wrong place. It should be:
        Code:
        gen wanted = datediff(dofC(registrationdate), daily("`c(current_date)'", "DMY"), "d")
        Last edited by Clyde Schechter; 22 Jun 2023, 16:50.

        Comment


        • #5
          thats worked now

          thanks, appreciated

          Comment


          • #6
            Hello @Clyde Schechter, I also need help please. I am working with a dataset which has a start and end time var in the format of 18:11:00.000+03:00. Please help me to (i) separate the tz (+03:00) from the time and (ii) calculate the difference between the end and start time. Thank you.
            Last edited by Emmanuel Wayagi; 22 Jan 2024, 07:22.

            Comment


            • #7
              Are all of your times in the same time zone? If not, will you ever need to compare times that are in different time zones? And are the start and end time in a given observation always in the same time zone? Finally, when you split out the time zone, what do you want to do with the time? That is, do you want to record it as GMT, or some other particular time zone, for all observations. Or do you just want to record at as the time in whatever timezone it was originally recorded as?

              Comment

              Working...
              X