Announcement

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

  • Assistance with dow command

    Hello, I am working with a dataset from 2016 and would like to identify the day of the week from the date variable (date), which provides date in ymd format (i.e. 20160514). I attempted the code:
    format date %td
    gen DOW = dow(date)
    but it only generated missing values for the DOW variable. I would greatly appreciate any insight to this code, thank you very much.

  • #2
    Something like this:

    Code:
    clear
    input str8 date
    "20160514"
    "20160516"
    "20160517"
    "20160518"
    end
    
    gen date1 = date(date, "YMD")
    format date1 %td
    gen DOW = dow(date1)
    But depends rather on you starting data. In the future, please post an example of your data with dataex, see the FAQ on how and why: https://www.statalist.org/forums/help#stata. The answer could be different depending on the variable type that holds your dates. Such guesswork can be prevented with a dataex example.
    Last edited by Jorrit Gosens; 06 Feb 2019, 08:16.

    Comment


    • #3
      If you're feeding in a numeric value such as 20160514 then that as a daily date is approximately

      Code:
      . di 20160514 / 365.25
      55196.479
      .
      55,000 years after 1960.

      Spelling out a display format %td does not convert a numeric variable into a Stata daily date; on the contrary, it asserts that your variable already is a Stata daily date.

      The problem here is not with the dow() function (not a command), but that your numeric dates make no sense. You as a person know what they mean, but that's not the issue.

      No shortcuts here, beyond the part of

      Code:
      help datetime
      that explains.

      Jorrit's post explains what to do. If your data are, as I guess, numeric already, you need the extra step of pushing them through string().


      Comment


      • #4
        Yeah, your use of
        Code:
        format date %td
        if without an error code would suggest your date variable is numeric. In that case it would be:

        Code:
        clear
        input long datenum
        20160514
        20160516
        20160517
        20160518
        end
        
        tostring datenum, gen(date)
        gen date1 = date(date, "YMD")
        format date1 %td
        gen DOW = dow(date1)

        Comment


        • #5
          Thank you both so much. Based on your input, I ran this code:
          Code:
           . tostring V5005, generate (date) 
          date generated as str8
          . gen date1=date(date, "YMD")
          . format date1 %td
          . gen DOW=dow(date1)
          And it quickly converted my dates to days of week. Thank you!

          Comment


          • #6
            That works, but what I was implying was more direct

            Code:
             
             gen date1=date(string(V5005), "YMD")

            Comment

            Working...
            X