Announcement

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

  • Create weekday variable from dates

    I have a variable with the date DDMMYYYY HM and need to create a new variable with the weekday:

    Code:
    date_interv
    21022018 1320
    02112016 1310
    18042018 0845
    06042016 1130
    20112018 1930
    11042016 1200
    17022016 1105
    13092016 0805
    31052017 0000
    26062017 1700
    03102016 1340
    14102016 0850
    17062016 1325
    05122017 1535
    Could anyone please guide me?
    Last edited by Lana Kim; 27 Feb 2022, 10:05. Reason: dates, varaibles

  • #2
    I am assuming you variable date_interv is a string variable containing the digits and space shown. Note that I include your example data in my code as the output of the dataex command. When you present example data using dataex, those who want to help you don't have to make uninformed guesses about the actual nature of your data. I added an initial observation that is the date and time that I am typing this to make it easy to check that I'm getting the right answer.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str13 date_interv
    "27022022 1200"
    "21022018 1320"
    "02112016 1310"
    "18042018 0845"
    "06042016 1130"
    "20112018 1930"
    "11042016 1200"
    "17022016 1105"
    "13092016 0805"
    "31052017 0000"
    "26062017 1700"
    "03102016 1340"
    "14102016 0850"
    "17062016 1325"
    "05122017 1535"
    end
    
    // first translate your string into a Stata Internal Format datetime variable
    generate double dt = clock(date_interv,"DMYhm")
    format %tc dt
    // use dofc() to extract the date part and dow() for the day of week 0=Sunday through 6=Saturday
    generate weekday = dow(dofc(dt))
    // what do we have
    list, clean noobs
    Code:
    . list, clean noobs
    
          date_interv                   dt   weekday  
        27022022 1200   27feb2022 12:00:00         0  
        21022018 1320   21feb2018 13:20:00         3  
        02112016 1310   02nov2016 13:10:00         3  
        18042018 0845   18apr2018 08:45:00         3  
        06042016 1130   06apr2016 11:30:00         3  
        20112018 1930   20nov2018 19:30:00         2  
        11042016 1200   11apr2016 12:00:00         1  
        17022016 1105   17feb2016 11:05:00         3  
        13092016 0805   13sep2016 08:05:00         2  
        31052017 0000   31may2017 00:00:00         3  
        26062017 1700   26jun2017 17:00:00         1  
        03102016 1340   03oct2016 13:40:00         1  
        14102016 0850   14oct2016 08:50:00         5  
        17062016 1325   17jun2016 13:25:00         5  
        05122017 1535   05dec2017 15:35:00         2
    Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

    All Stata manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

    Added in edit: Regarding the advice about using dataex, it is possible that the example data was actually already a datetime variable with a custom format.
    Code:
    . generate double dt = clock(date_interv,"DMYhm")
    
    . format %tcDDNNCCYY_HHMM dt
    
    . // what do we have
    . list, clean noobs
    
          date_interv              dt  
        27022022 1200   27022022 1200  
        21022018 1320   21022018 1320  
        02112016 1310   02112016 1310  
        18042018 0845   18042018 0845  
        06042016 1130   06042016 1130  
        20112018 1930   20112018 1930  
        11042016 1200   11042016 1200  
        17022016 1105   17022016 1105  
        13092016 0805   13092016 0805  
        31052017 0000   31052017 0000  
        26062017 1700   26062017 1700  
        03102016 1340   03102016 1340  
        14102016 0850   14102016 0850  
        17062016 1325   17062016 1325  
        05122017 1535   05122017 1535
    Also, it is possible that there was more than one space between the date and time portions - the forum software strips out extra spaces for text that is not surrounded by code delimiters [CODE] and [/CODE].
    Last edited by William Lisowski; 27 Feb 2022, 11:14.

    Comment

    Working...
    X