Announcement

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

  • Help to transform hourly format to daily format

    I have a database with dates in hourly format, and I need to transform them into daily format. Could someone please help me? Thank you in advance!

  • #2
    Hi Mario, welcome to the forum.

    I'm not sure what you mean by "hourly format" or "daily format". Check the documentation under

    Code:
    help datetime display formats
    The documentation lists 8 data types and the corresponding display format.

    Code:
        The formats for displaying Stata dates and times are
    
             Stata data type    Display format
             -------------------------------------
             datetime/c            %tc[details]
             datetime/C            %tC[details]
    
             date                  %td[details]
    
             weekly date           %tw[details]
             monthly date          %tm[details]
             quarterly date        %tq[details]
             half-yearly date      %th[details]
             yearly date           %ty[details]
             -------------------------------------
    Let's suppose you have a datetime data type, which would make sense given that you are talking about hours. You don't provide any example data (as you are generally asked to) so I will randomly generate five dates and give them the default format.

    Code:
    clear
    set obs 5
    set seed 128493
    
    gen second = runiformint(0, 59)
    gen minute = runiformint(0, 59)
    gen hour = runiformint(0, 23)
    gen day = runiformint(1, 28)
    gen month = runiformint(1, 12)
    gen year = runiformint(1970, 2023)
    gen datetime = mdyhms(month, day, year, hour, minute, second)
    
    format %tc datetime
    list datetime
    Code:
    . list datetime
    
         +--------------------+
         |           datetime |
         |--------------------|
      1. | 22jan2022 03:53:47 |
      2. | 21mar1990 19:52:17 |
      3. | 17mar1995 17:53:28 |
      4. | 18aug2002 00:04:56 |
      5. | 22oct2005 04:45:24 |
         +--------------------+
    So how exactly do you want to format this? Do you only want to see the day, month, and year?

    Code:
    format %tcDD_mon_CCYY datetime
    list datetime
    Code:
    . list datetime
    
         +-------------+
         |    datetime |
         |-------------|
      1. | 22 jan 2022 |
      2. | 21 mar 1990 |
      3. | 17 mar 1995 |
      4. | 18 aug 2002 |
      5. | 22 oct 2005 |
         +-------------+

    Comment


    • #3
      Thank you very much Daniel, with your explanation I got to solve the problem

      Comment

      Working...
      X